JAG Practice Contest for ACM-ICPC Asia Regional 2016 C题【贪心】

camp给出的题解:

题解:贪心,先算出最小需要的长度。然后从左到右依次确定每一位。复杂度O(n)O(n)

长度为 2n2n 的串可以构造出需要 [0,1+3+...+2n-1][0,1+3+...+2n1] 中所有的数,所以长度是单调的。
从左到右贪心着放的时候,右边的A的upper bound就是先放)再放(,这个的upper bound可以O(1)算出来

其实代码:

发现自己的贪心好弱。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
	int n;
	cin>>n;
	int res;
	for(int i=1;;i++)
    {
		if(i*(i+1)/2>=n){
			res = i*2;
			break;
		}
	}
	string s=string(res/2,')')+string(res/2,'(');
	swap(s[res/2],s[res/2-((res/2*(res/2+1)/2)-n)]);
	cout<<s<<endl;
	return 0;
}


原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934737.html