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. Windows下OpenFOAM开发及使用环境配置指南 (1)【转载】

    转载自:http://openfoam.blog.sohu.com/158614863.html *************************************************** ...

  2. CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)

    You are given a function ff written in some basic language. The function accepts an integer value, w ...

  3. 基于Spring框架怎么构建游戏玩法服务

    说明:本篇阐述的问题,是基于前面的游戏服务器架构设计的. 问题 众所周知,Spring最擅长的领域是无状态服务的构建,而游戏(尤其是玩法部分)是有状态的.以棋牌游戏为例,玩法服务里面大概涉及以下两类对 ...

  4. Redis客户端多线程跟多个连接不是一回事

    先抱怨一波,大国庆节的放假前一天的下班前15分钟,通知让我加班,因为一个Redis的bug,而这个bug我在1个半小时之前刚听说了个大概,心里很不情愿: 好了,说正事: 问题现象: bug是这样的,两 ...

  5. WebSocketSharp中WebSocket类

    websocket-sharp.clone, Version=1.0.2.39869 WebSocket由方法调用事件改为实例化委托调用,两种构造 1.构造函数 第一种 // // 摘要: // In ...

  6. 4.弹性网络( Elastic Net)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_21904665/article/details/52315642 ElasticNet 是一种 ...

  7. 开发WINDOWS服务程序

    开发WINDOWS服务程序 开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了,打开Service1窗口,有几个属性说明 ...

  8. Python问题:error: Microsoft Visual C++ 9.0 is required

    Python问题:error: Microsoft Visual C++ 9.0 is required 原因是缺少编译C的 VCForPython包. 解决办法: 安装VCForPython即可. ...

  9. hadoop 2.7.3伪分布式环境运行官方wordcount

    hadoop 2.7.3伪分布式模式运行wordcount 基本环境: 系统:win7 虚机环境:virtualBox 虚机:centos 7 hadoop版本:2.7.3 本次以伪分布式模式来运行w ...

  10. git 初次提交

    前提:已经关联了ssh到本地,不用再输入用户名和密码 知道仓库地址:https://xxx.xxx.com/aiotcloud/yihao01-node-js.git 第一步:进入本地文件夹执行 gi ...