【规律】Parentheses
Parentheses
题目描述
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 ')'.
输入
输出
样例输入
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的更多相关文章
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode]题解(python):032-Longest Valid Parentheses
题目来源 https://leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the cha ...
- Generate Parentheses java实现
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- BZOJ 4706: B君的多边形 找规律
4706: B君的多边形 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4706 Description 有一个正n多边形,我们要连接一些 ...
- CSU - 1542 Flipping Parentheses (线段树)
CSU - 1542 Flipping Parentheses Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld ...
- 【LeetCode】【动态规划】Generate Parentheses(括号匹配问题)
描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍
算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...
随机推荐
- Mac 中使用phpstorm 修改文件提示"only read",只读权限
在终端中执行命令: 给max系统用户liutao赋予整个项目文件权限,即可成功 sudo chown -R liutao /Users/liutao/Desktop/vagrant/newprojec ...
- SQL中如何使用方向键——lrwrap
Linux alias命令用于设置指令的别名. 用户可利用alias,自定指令的别名.若仅输入alias,则可列出目前所有的别名设置.alias的效力仅及于该次登入的操作.若要每次登入是即自动设好别名 ...
- 002-创建型-03-单例模式(Singleton)【7种】、spring单例及原理
一.概述 保证一个类仅有一个实例,并提供一个全局访问点 私有构造器.线程安全.延迟加载.序列化和反序列化安全.反射攻击 1.1.适用场景 1.在多个线程之间,比如servlet环境,共享同一个资源或者 ...
- 012-多线程-JUC集合-Queue-SynchronousQueue和LinkedTransferQueue
一.SynchronousQueue概述 SynchronousQueue是一个不存储元素的队列.每一个put操作必须等待一个take操作,否则不能继续添加元素. 它支持公平访问队列.默认情况下线程采 ...
- Qt 获取键盘输入
void vc_widget::keyPressEvent(QKeyEvent *ev) { if (ev->key() == Qt::Key_Up) { qDebug() << & ...
- 24Flutter中常见的表单有TextField单行文本框,TextField多行文本框、CheckBox、Radio、Switch
一.Flutter常用表单介绍: CheckboxListTile.RadioListTile.SwitchListTile.Slide. 二.TextField:表单常见属性: maxLines:设 ...
- 不同操作系统上DNS客户端操作区别汇总
结论:windows有DNS缓存,Linux默认无DNS缓存,只能依赖于安装其他软件. 一.不同操作系统的客户端的DNS缓存差别 1.windows 系统中dns 解析器会使用系统的dns缓存来提高d ...
- poi导出excel2007版本
/** * 导出excel2007版本 * * @param titles * 表头集合 * @param sheetNames * sheet名称 * @param datas * 数据集合 * @ ...
- Mysql备份工具mysqldump和mysqlhotcopy
(1).Mysql备份类型 1)按照备份时对数据库的影响分为 Hot backup(热备):也叫在线备份.指在数据库运行中直接备份,对正在运行的数据库没有任何影响. Cold backup(冷备):也 ...
- JavaScript正则表达式补充
定义正则表达式 /.../用于定义正则表达式 /.../g表示全局匹配 /.../i表示不区分大小写 /.../m表示多行匹配 JS正则匹配时本身就是支持多行,此处多行匹配只是影响正则表达式^和$,m ...