Codeforces 847 C. Sum of Nestings (技巧)
来源:互联网 发布:软件测试外包公司 编辑:程序博客网 时间:2024/05/21 14:07
Description
Recall that the bracket sequence is considered regular if it is possible to insert symbols ‘+’ and ‘1’ into it so that the result is a correct arithmetic expression. For example, a sequence “(()())” is regular, because we can get correct arithmetic expression insering symbols ‘+’ and ‘1’: “((1+1)+(1+1))”. Also the following sequences are regular: “()()()”, “(())” and “()”. The following sequences are not regular bracket sequences: “)(“, “(()” and “())(()”.
In this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence “()(())” the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·10^5, 0 ≤ k ≤ 10^18) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print “Impossible” (without quotes).
Examples input
3 1
Examples output
()(())
题意
给定
思路
我们考虑一种嵌套最大和的情况: ((((()))))
,此时
显然,对于
此时这部分的序列类似于 (((())()))
这样的结果,假如括号没有用完,我们在最后补单独的 ()
即可。
而对于 Impossible
,因为在最极端的情况下都无法找到解,因此其一定不成立。
AC 代码
#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+10;typedef __int64 LL;LL n,k;int main(){ cin>>n>>k; if(k>n*(n-1)/2) puts("Impossible"); else { string ans; int base = 0; LL num = 0; while(num+base<=k) { num+=base++; ans+="("; } LL cnt = k-num; num = 0; while(base--) { ans+=")"; if(base && base == cnt)ans+="()",num++; num++; } for(int i=0; i<n-num; i++) ans+="()"; cout<<ans<<endl; } return 0;}
- Codeforces 847 C. Sum of Nestings (技巧)
- CodeForces 847CSum of Nestings
- Codeforces847C Sum of Nestings
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- Codeforces Round #277.5 (Div. 2)---C. Given Length and Sum of Digits (贪心)
- Codeforces 489C - Given Length and Sum of Digits...(贪心)
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- 【Codeforces】 489C Given Length and Sum of Digits
- codeforces#277.5 C. Given Length and Sum of Digits
- CodeForces 489C Given Length and Sum of Digits...
- Codeforces 489C Given Length and Sum of Digits
- CodeForces-489C Given Length and Sum of Digits...
- Codeforces-489C-Given Length and Sum of Digits...
- Codeforces 489C. Given Length and Sum of Digits...(greedy)
- Codeforces 110C Lucky Sum Of Digits 暴力
- codeforces 489C Given Length and Sum of Digits...
- Codeforces 122 C. Lucky Sum(分块)
- 371. Sum of Two Integers(C++)
- STM32中的位变量实现
- 如何用TensorFlow训练聊天机器人(附github)
- spring @RequestBody和@RequestParam区别
- 幸福
- java基础之控制语句
- Codeforces 847 C. Sum of Nestings (技巧)
- sed命令实例
- 以管理员权限运行
- MVVM
- C# for循环倒序输出数组元素
- 【数据库】关系代数基本运算
- iOS录音
- 宝石与风景
- ROS下工作空间、功能包的建立,以及创建节点,实现节点之间的通信!