题意:给定一个括号的序列,原先的序列是碰到左括号加1,碰到右括号减1,然后把序列打乱,让你找出字典序最小的一个答案。

析:直接从第一个括号判断就好了,优先判断左括号,如果不行就加右括号。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];
map<int, int> mp; int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
mp.clear();
for(int i = 0; i < n; ++i) scanf("%d", a+i), ++mp[a[i]];
printf("Case %d: ", kase);
if((n&1) || !mp.count(1)){ printf("invalid\n"); continue; }
bool ok = true;
--mp[1];
int pos = 1;
for(int i = 1; i < n && ok; ++i){
if(mp.count(pos+1) && mp[pos+1]) ++pos, --mp[pos];
else if(mp.count(pos-1) && mp[pos-1]) --pos, --mp[pos];
else ok = false;
}
if(!ok && pos){ printf("invalid\n"); continue; }
for(int i = 0; i < n; ++i) ++mp[a[i]];
printf("(");
pos = 1;
for(int i = 1; i < n; ++i){
if(mp.count(pos+1) && mp[pos+1]) ++pos, --mp[pos], printf("(");
else if(mp.count(pos-1) && mp[pos-1]) --pos, --mp[pos], printf(")");
}
printf("\n");
}
return 0;
}

  

UVaLive 7637 Balanced String (构造)的更多相关文章

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

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

  2. uva live 7637 Balanced String (贪心)

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

  3. 贪心 UVALive 6832 Bit String Reordering

    题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...

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

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

  5. UVALive 6088 Approximate Sorting 构造题

    题目链接:点击打开链接 题意: 给定一个n*n的01矩阵 我们跑一下例子== 4 0111 0000 0100 0110 0123 \|____ 0|0111 1|0000 2|0100 3|0110 ...

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

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

  7. 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 ...

  8. LeetCode 1234. Replace the Substring for Balanced String

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

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

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

随机推荐

  1. 九度OJ 1069:查找学生信息 (排序、查找)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11240 解决:3024 题目描述: 输入N个学生的信息,然后进行查询. 输入: 输入的第一行为N,即学生的个数(N<=1000) 接 ...

  2. mac上完整卸载删除.简单粗暴无脑:androidstudio删除方案

    如果你是mac  ,你删除as ,删不干净也正常,你会发现安装的时候,前面的东西也在.配置文件在,会导致你以前的错误不想要的东西都在. 废话不多说,复制粘贴就是干!!!!~~~~~~~~ 第一步: 复 ...

  3. python 创建一个实例:步骤二 添加行为方法,编写方法

    添加方法 class Person(): def __init__(self,name,job=None,pay=0): self.name= name self.job = job self.pay ...

  4. LeetCode 017 4Sum

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  5. ABAP alv report

    *&---------------------------------------------------------------------* *& Report  YTST_FF_ ...

  6. Machine Learning No.8: Clusting

    1. K-means algorithm 2. K-means optimization objective 3. Random initialization

  7. 重新实践c++primer上面的代码

    又重新敲了敲c++primer上面的代码,觉得很有意思,讲的很细,c++真牛逼啊 #include <iostream> #include <string> #include ...

  8. 加州小学grade1,学习计划

    Visual vocabulary Grammar Spelling Maths Chapter 1 Patterns and Number SenseChapter 2Understanding A ...

  9. ES6 Set数据结构

    Set ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. const s = new Set(); // const声明一个只读的常量.一旦声明,常量的值就不能 ...

  10. jQuery Validate 插件为表单提供了强大的验证功能

    之前项目开发中,表单校验用的jQuery Validate 插件,这个插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的 ...