UVaLive 7637 Balanced String (构造)
题意:给定一个括号的序列,原先的序列是碰到左括号加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 (构造)的更多相关文章
- UVALive - 7637 E - Balanced String(构造)
原题链接 题意:给出一个打乱顺序的序列,问是否能构造出一个括号匹配的字符串.每个数字为此前读取到的左括号数减去右括号数. 分析:有左括号开始构造,不够的话就找右括号.注意特殊情况待处理.详情看代码 # ...
- uva live 7637 Balanced String (贪心)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 贪心 UVALive 6832 Bit String Reordering
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...
- codeforces 709D D. Recover the String(构造)
题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...
- UVALive 6088 Approximate Sorting 构造题
题目链接:点击打开链接 题意: 给定一个n*n的01矩阵 我们跑一下例子== 4 0111 0000 0100 0110 0123 \|____ 0|0111 1|0000 2|0100 3|0110 ...
- java的String构造对象的几种方法以及内存运行过程
String类创建对象的方法可以分为以下三种 1.String a = "123"; 2.String b = new String("123"); 3.Str ...
- 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 ...
- LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...
- AtCoder Grand Contest 032 B - Balanced Neighbors——构造
题意 B - Balanced Neighbors 给定一个整数 $N$($3\leq N \leq 100$),构造一个顶点编号为 $1...N$ 的无向图,需满足如下两个条件: 简单图且连通 存在 ...
随机推荐
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之_关于接口调用常见的一些问题(401 Unauthorized)
在之前的博客<EasyNVR H5流媒体服务器方案架构设计之视频能力平台>中我们描述了EasyNVR的定位,作为一个能力平台来进行功能的输出: 也就是说,在通常情况下,我们将一套视频的应用 ...
- 九度OJ 1020:最小长方形 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...
- regularexpression_action
re.compile('"ssid":"[^"]*"}',re.MULTILINE) regex ,str_= re.compile('"s ...
- git版本控制-- Windows+Git+TortoiseGit+COPSSH安装图文教程
Windows+Git+TortoiseGit+COPSSH 安装图文教程 教程网址: http://www.liaoxuefeng.com/wiki/0013739516305929606dd183 ...
- ABap-小技巧
if FIELD cn '0123456789'. *&如果字符串包含‘数字’ STOP. endif. 同理到字母‘ABCDEFG*’ 'abcdefg*' '/' '\' 等其它字 ...
- .net编程扫盲(*)
http://www.cnblogs.com/edisonchou/p/4787775.html
- 用JAVA 的for循环输出 菱形
public class For{ public static void main(String[] args){ //首先.把菱形看成上下两部分,上五下四,所以,第一个for循环有5次,第二个for ...
- 关于Python有用的snippets
1.将字典的key,value反转换位置 值value可以取任何数据类型,但键key必须是不可变的,如字符串,数字或元组. dict1={'Lisa':1,'Bob':2,'Mick':3} dict ...
- Kattis - cardhand Card Hand Sorting 【暴力枚举】
题意 给出 一个扑克牌的序列 求排成一个"有序"序列 最少的插入次数 有序是这样定义的 同一个花色的 必须放在一起 同一花色中的牌 必须是 升序 或者是 降序 然后 A 是最大的 ...
- Linux常用命令全集
一,安装和登陆命令1,进入图形界面 startx 2,进入图形界面 init 5 3,进入字符界面 init 34,登陆 login 5,关机 poweroff -p 关闭机器的时候关闭电源-n 在关 ...