【规律】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道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...
随机推荐
- 2019-12-11:kali linux工具Msfvenom 命令自动补全
msfvenom大家都不陌生,在我们使用MSF进行权限维持,内网渗透的时候都会用到,支持的语言的种类很多.大家都知道我们在使用msfvenom 的时候需要手动输入很多参数,这些参数需要记忆,或记在其它 ...
- 深入理解JVM虚拟机6:深入理解JVM类加载机制
深入理解JVM类加载机制 简述:虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 下面我们具体 ...
- mac使用vnc远程登录ubuntu16.04桌面
mac使用vnc远程登录ubuntu16.04桌面 1.安装在Ubuntu上安装x11vnc,如下: sudo apt-get install x11vnc 2.配置vnc密码 x11vnc -sto ...
- layui跨域问题的解决
跨域问题的解决 由于浏览器存在同源策略,所以如果 layui(里面含图标字体文件)所在的地址与你当前的页面地址不在同一个域下,即会出现图标跨域问题.所以要么你就把 layui 与网站放在同一服务器 ...
- MediaPlayer: MediaPlayer中的prepare方法和prepareAsync方法的区别
prepare方法是将资源同步缓存到内存中,一般加载本地较小的资源可以用这个,如果是较大的资源或者网络资源建议使用prepareAsync方法,异步加载.但如果想让资源启动,即start()起来,因为 ...
- SQL-W3School-函数:SQL AVG() 函数
ylbtech-SQL-W3School-函数:SQL AVG() 函数 1.返回顶部 1. 定义和用法 AVG 函数返回数值列的平均值.NULL 值不包括在计算中. SQL AVG() 语法 SEL ...
- jq删除标签
<script>$(function(){ $("div").remove()})</script>
- 【Leetcode_easy】669. Trim a Binary Search Tree
problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完
- Docker - 在CentOS7中安装Docker
在CentOS 7中安装Docker 1-确认系统信息 # cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # uname - ...
- Docker 容器的运行(八)
目录 一.运行容器 1.运行第一个容器 2.让容器长期运行 二.进入容器 1.attach 2.exec 3.attach VS exec 4.容器内部都在干些什么 三.停止/启动/重启容器 四.暂停 ...