Parentheses

题目描述

Dave loves strings consisting only of '(' and ')'. Especially, he is interested in balanced strings. Any balanced strings can be constructed using the following rules:

A string "()" is balanced.
Concatenation of two balanced strings are balanced.
If T is a balanced string, concatenation of '(', T, and ')' in this order is balanced. For example, "()()" and "(()())" are balanced strings. ")(" and ")()(()" are not balanced strings.
Dave has a string consisting only of '(' and ')'. It satises the followings:

You can make it balanced by swapping adjacent characters exactly A times.
For any non-negative integer B (B<A), you cannot make it balanced by B swaps of adjacent characters.
It is the shortest of all strings satisfying the above conditions.
Your task is to compute Dave's string. If there are multiple candidates, output the minimum in lexicographic order. As is the case with ASCII, '(' is less than ')'.

输入

The input consists of a single test case, which contains an integer A (1≤A≤109).

输出

Output Dave's string in one line. If there are multiple candidates, output the minimum in lexicographic order.

样例输入

1

样例输出

)(

提示

There are infinitely many strings which can be balanced by only one swap. Dave's string is the shortest of them.


【题意】

  请大家找到一个通过交换两个位置的操作,经过n次这样操作,能把括号序列变成合法。

  首先保证最短,其次保证字典序最小。

【题解】

  1:  ")("

  3:  "))(("

  6:  ")))((("

  10:  "))))(((("

  2:  ")()("

  4:  "))()(("

  5:  ")())(("

  

根据上面的情况找出的规律是:

  等差数列的位置必定是  " )))((("这样的,但是如果不是等差数列则需要在某个位置上交换一下。

  “)())((”

  "))()(("


 #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = ;
typedef pair<ll,ll> pii;
inline ll read(){ll x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<='') x=x*+ch-'',ch=getchar();return x*f;} const int N=1e7+; int main()
{
ll n;
scanf("%lld",&n);
int pos = sqrt(*n);
while(pos*(pos+) > *n) pos--;
//cout<<pos<<endl;
if(pos*(pos+)/ == n){
for(int i=;i<=pos;i++) printf(")");
for(int i=;i<=pos;i++) printf("(");
printf("\n");
//main();
return ;
}
int dis = n - pos*(pos+)/;
//cout<<dis<<endl; for(int i=;i<=dis;i++) printf(")");
printf("(");
for(int i=dis;i<=pos;i++) printf(")");
for(int i=;i<=pos;i++) printf("(");
printf("\n");
//main();
return ;
}

【规律】Parentheses的更多相关文章

  1. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  2. [LeetCode]题解(python):032-Longest Valid Parentheses

    题目来源 https://leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the cha ...

  3. Generate Parentheses java实现

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  5. BZOJ 4706: B君的多边形 找规律

    4706: B君的多边形 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4706 Description 有一个正n多边形,我们要连接一些 ...

  6. CSU - 1542 Flipping Parentheses (线段树)

    CSU - 1542 Flipping Parentheses Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld ...

  7. 【LeetCode】【动态规划】Generate Parentheses(括号匹配问题)

    描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  9. 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍

    算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...

随机推荐

  1. 雪花算法(DELPHI实现)

    雪花算法(DELPHI实现) 生成ID能够按照时间有序生成. 分布式系统内不会产生重复id(用workerId来做区分). 自增ID:对于数据敏感场景不宜使用,且不适合于分布式场景. GUID:采用无 ...

  2. Python JSON 字符串 转 json 基本使用

    字符串 转  json import json jsonData = '{"a":1,"b":2,"c":3,"d":4 ...

  3. "Could not resolve host: mirrorlist.centos.org; Unknown error"解决方法

    这两天学习历程可谓历尽坎坷,昨天在vSphere Client中安装完CentOS系统后,今天尝试在系统中安装mysql数据库. 由于刚接触Linux,所以对于一些常用指令和操作并不熟悉,也是一边百度 ...

  4. ISO/IEC 9899:2011 条款5——5.2.1 字符集

    5.2.1 字符集 1.两个字符集和它们相关联的依次顺序应该被定义:写在源文件中的集合(源字符集),以及在执行环境中被解释的集合(执行字符集).每个集合此外被划分为一个基本字符集,其内容由本子条款给出 ...

  5. 【集成模型】Boosting

    0 - 思想 Bagging算法思想是减少预测方差(variance),Boosting算法思想是为了减少预测偏差(bias). Boosting算法思想是将“弱学习算法”提升为“强学习算法”.一般来 ...

  6. OSError: image file is truncated (28 bytes not processed)

    解决办法: 在代码中添加两行 from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True

  7. VBScript把json字符串解析成json对象的2个方法

    这篇文章主要介绍了VBScript把json字符串解析成json对象的2个方法,本文通过MSScriptControl.ScriptControl和jscript实现,需要的朋友可以参考下 asp/v ...

  8. The pom for XXX is missing,no dependency information available

    笔者进行性能测试时,碰到如下问题 性能测试代码编写,调试通过之后.使用cmd进入项目根目录,意图打包导出项目中所有的依赖包,以便导入至jmeter工具中 cmd中使用命令:mvn dependency ...

  9. kafka和zookeeper的配置文件优化配置

    zookeeper的配置 日志自动清理这两个参数都是在zoo.cfg中配置的:    autopurge.purgeInterval 这个参数指定了清理频率,单位是小时,需要填写一个1或更大的整数,默 ...

  10. PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)

    1045 Favorite Color Stripe (30 分)   Eva is trying to make her own color stripe out of a given one. S ...