Parentheses

Parentheses
Input: Standard Input
Time Limit: See AtCoder
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 satis es 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)’.
Input
The input consists of a single test case, which contains an integer A(1 <= A <=  109).
Output
Output Dave’s string in one line. If there are multiple candidates, output the minimum in lexicographic order.

Sample Input 1
1

Output for the Sample Input 1
)(

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

Sample Input 2
4

Output for the Sample Input 2
)())((

String ))(()(” can be balanced by 4 swaps, but the output should be )())((” because it is the minimum in lexicographic order.

题意:
在一个只有‘(’,‘)’的字符串中,()是平衡的,连续的平衡字符串也是平衡的,给你一个数字a,问你怎样的字符串要通过最少a次交换相邻的字符才能变为平衡的。

分析:
这题我写时一遍直接AC了,哈哈哈。。。

这种题目一看就是要慢慢列出来然后找规律:
a=1 : )(
a=2 : )()(
a=3 : ))((
a=4 : )())((
……
a=6 : )))(((
写出了这么多后,我们会发现这样一个规律,当上一个存在()时,只要将第一个()相互交换位置就可以使最少交换次数加一。当不存在()时,就在最前面加上)(即可使最小交换次数加一。
交换第一个 ()和在前面加)(可以使其字典序最小

所以现在我们可以从一开始模拟就可以求出所以的了,但是会超时。
我们还发现不存在()的情况很特殊,))(( 的最少交换次数为(2 * 3) / 2 ,)))(((的最少交换次数为(3 * 4) / 2…… 所以t个)和t个(构成的字符串最小的交换次数为(t*(t+1))/2

最后我们先求出t*(t+1)<=2*a 的最大的t,然后在进行处理就可以了。

AC代码:

#include <iostream>
using namespace std;
int main()
{
	int a ;
	while(cin >> a)
	{
		int t = 1 ;
		while(t * (t + 1) <= 2 * a) t ++ ;
		t -- ;
		if(t * (t + 1) == 2 * a) 
		 {
		 	t *= 2 ;
		 	for(int i = 1;i <= t / 2 ;i ++)
		 	 printf(")") ;
		 	for(int i = 1 ;i <= t / 2;i ++)
		 	 printf("(") ;
		 	puts("") ;
		 }
		 else 
		 {
		 	
		 	int i = 1 ;
		 	int c = a - t * (t + 1) / 2 ;
		 	for( ;i <= c ;i ++)
		 	 printf(")") ;
		 	printf("(") ;
		 	i ++ ;
		 	for( ;i <= t + 2 ;i ++)
		 	 printf(")") ;
		 	for( int i = 1 ;i <= t ;i ++)
		 	 printf("(") ;
		 	puts("") ;
		 }
	}
	return 0 ;
}
全部评论

相关推荐

程序员花海_:抓紧时间去找实习 项目其实只是玩具项目 脱离业务很远的
点赞 评论 收藏
分享
真心劝退测开,这个方向真的不适合普通人,尤其是应届生。我身边这一届同学的情况,说实话已经很说明问题了。后端秋招一开始确实难,但只要技术不是太拉,后面补录、加面、捞人的机会一波接一波,最后基本都能上岸中小厂。而那些一开始就冲测开的,很多到现在还在等消息,甚至直接凉了。最直观的感受就是:测开的坑真的少得可怜。同一批同学里,后端、前端、客户端基本都有大厂&nbsp;offer&nbsp;扎堆的情况,哪怕不是顶级大厂,也能拿到几个中厂保底。但测开呢?泡出来的又有多少呢。不是不努力,是岗位就那么点,连给你复活赛的机会都没有。后端还能互相捞。秋招挂了,春招、补录、内推、转组,总能找到出口。测开一旦挂了,基本就是真的挂了,后面连投的岗位都没几个。目前有些转的人可能拿了几个不错的实习offer,那到秋招呢?hc少就笑不出来了。现在测开也就只有大厂和顶中厂有,小厂就是测试点点点,大厂也很多是点点点,后端起码还有小厂保个底还有人幻想什么先测开再转开发,我只能说太天真了。测开的经历,想转后端或者客户端根本不可能。核心开发经验没有,项目深度不够,面试官一句那你为什么不一开始就做开发基本宣判死刑。反过来,后端、前端干不下去了,转测开却很容易,这已经说明问题了。如果你是普通双非,那更要慎重。测开&nbsp;HC&nbsp;本来就少,筛人还看背景,普通学校在这种极小池子里基本就是陪跑。你用一个最普通的简历,去抢最少的岗位,结果可想而知。再说客户端和前端。很多人看不起前端,觉得卷,觉得不高级,但现实是岗位多、需求稳定、HC&nbsp;实在。客户端更不用说,Android&nbsp;和&nbsp;iOS&nbsp;到现在依然是硬需求,技术路线清晰,工程经验越久越值钱。我身边拿到大厂最多的,反而是客户端和前端,而不是测开。说句难听的,测开不是不能干,但那是给已经没得选的人准备的退路,不是给应届生拿来当首选的。秋招无脑选测开,本质就是用短期好像更容易上岸,换长期被动甚至被锁死。我是真心建议,能选客户端和前端就选客户端前端,再不行就去后端,哪怕多投多卷一点,也比一头扎进测开强得多。等你真正经历一轮秋招、春招、补录之后,就会发现被反复捞的,从来不是测开劝退不是唱衰,是不想看更多人踩已经踩烂的坑。
Java抽象小篮子:这话术换成劝退后端开发一点问题也没有,总有小登冲出来说别人想焊死车门,我寻思车门要真这么容易焊丝还轮得到你们上车吗
计算机有哪些岗位值得去?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务