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. mysql解压文件安装

    mysql.zip版本的安装教程   MySQL zip版本安装 一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在M ...

  2. lucene反向索引——倒排表无论是文档号及词频,还是位置信息,都是以跳跃表的结构存在的

    转自:http://www.cnblogs.com/forfuture1978/archive/2010/02/02/1661436.html 4.2. 反向信息 反向信息是索引文件的核心,也即反向索 ...

  3. git submodule 如何push代码

    某git项目关联了一个submodule 如何更新该项目下的submodule https://stackoverflow.com/questions/5814319/git-submodule-pu ...

  4. flutter doctor出现问题 [!] Android toolchain - develop for Android devices (Android SDK version 28.0.3) X Android license status unknown. Try re-installing or updating your Android SDK Manager. 的解决方案

    首先,问题描述: flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Cha ...

  5. java spark list 转为 RDD 转为 dataset 写入表中

    package com.example.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.Hash ...

  6. 如何确认oracle客户端中的TNSNAMES中的service_name

    在我们安装oracle的客户端,或者PLSQL develop的时候,需要在oracle的安装目录下修改TNSNAME中配置你要连接的数据库的TNS参数.配置好了可以通过命令行使用tnsping命令测 ...

  7. Ubuntu + Apache2 环境下用C编写 一个简单的cgi脚本

    我只学习过c语言,没有学习过prel,网上很多教程都是针对prel的,很少有针对c的.自己在Ubuntu下鼓捣了一下午,也总算是用c成功编写了一个helloworld的cgi,算是cgi入门的第一步. ...

  8. NPM 私服

    下载https://nodejs.org/en/download/ linux binaries x64版本xz -d ....xztar -xvf ....tar导入path修改~/.bashrc加 ...

  9. flutter showDatePicker显示中文日期_Flutter时间控件显示中文

    flutter showDatePicker showTimePicker显示中文日期 1.配置flutter_localizations依赖 找到pubspec.yaml配置flutter_loca ...

  10. zip格式压缩文件辅助类(ZipHelper)

    /// <summary> /// 压缩解压文件 /// </summary> public class ZipHelper { /// <summary> /// ...