题意:给定一个括号的序列,原先的序列是碰到左括号加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. animate和scrollTop的使用

    // 平滑滚动到ola结果位置 var scrollHeight = $("#wrap_div")[0].scrollHeight; var curDivHeight = $(&q ...

  2. 我的Android进阶之旅------>使用ThumbnailUtils类获取视频的缩略图

    今天看了一段代码,是关于获取视频的缩略图的,让我认识了一个ThumbnailUtils类,代码如下. Bitmap bitmap = ThumbnailUtils.createVideoThumbna ...

  3. git 从远端拉取指定分支和推送本地某个分支到远端

    如题,可以直接从远端拉取某个分支,也可以直接将本地某个分支推送到远端. 原文链接:https://www.cnblogs.com/hamsterPP/p/6810831.html

  4. Java for LeetCode 132 Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. vscode 中 eslint 的配置

    上个博客说到了如何配置 eslint 和 editorconfig, 但是每次运行项目的时候才能知道哪段代码不符合规范,确实也有点蛋疼. 那么能不能直接在编辑器中就能看到不符合规范的代码的呢??? 这 ...

  6. packstack快速安装

    1 安装软件库 更新安装的软件包,命令如下: sudo yum update -y 建立RDO库,命令如下: sudo yum install -y https://rdo.fedorapeople. ...

  7. 使用pip安装第三方库报错记录

    今天在使用pycharm导入第三方库的时候,报了好多超时错误,还有标题中的找不到版本,应该是网络的原因,记录下解决的办法: raise ReadTimeoutError(self._pool, Non ...

  8. Appium——api常用函数

    appium常用函数介绍:   获取页面信息:   1. def get_current_activity(cls, driver): ''' 获取当前页面的activity :param drive ...

  9. zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)

    1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...

  10. windowService中使用多线程

    windowService中使用多线程 代码 using System;using System.Collections.Generic;using System.Linq;using System. ...