Codeforces847C Sum of Nestings
来源:互联网 发布:动漫电影推荐 知乎 编辑:程序博客网 时间:2024/06/05 02:31
标签:dfs
Recallthat the bracket sequence is considered regular if it is possible to insertsymbols '+' and '1' into it so that the result is a correctarithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmeticexpression 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 thisproblem you are given two integers n and k. Your task is to construct a regularbracket sequence consisting of round brackets with length 2·n with total sum ofnesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to thenumber of pairs of brackets in which current opening bracket is embedded.
Forexample, in the sequence "()(())" thenesting of first opening bracket equals to0, the nesting of the second opening bracket equals to 0 and the nesting of the third openingbracket equal to 1. So the total sum ofnestings equals to 1.
Input
The firstline contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) —the number of opening brackets and needed total nesting.
Output
Print therequired regular bracket sequence consisting of round brackets.
If thereis no solution print "Impossible"(without quotes).
Example
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The firstexample is examined in the statement.
In thesecond example the answer is "(((())))".The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6.
In thethird it is impossible to construct a regular bracket sequence, because themaximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained forthe sequence "(())".
直接递归搜索即可,注意要先搜索是否可行,如果不行的话输出impossible,最后再输出
Code
#include<bits/stdc++.h>#define rep(i,a,b) for(int i=a;i<=b;i++)#define dep(i,a,b) for(int i=a;i>=b;i--)#define LL long longusing namespace std;long long n,k;int cnt[500005];void f(int x,int y){if(x==y+1)return;printf("(");f(x+1,y);printf(")");while(cnt[x]--)printf("()");}int main(){long long base=0; cin>>n>>k; rep(i,0,n-1){ base+=i; if(base<=k&&k<=base+(n-i-1)*i){ k-=base; n-=i+1; dep(x,i,0) while(n&&k-x>=0){ cnt[x]++; k-=x; n--; } f(0,i); return 0; } } printf("Impossible\n"); return 0;}
- Codeforces847C Sum of Nestings
- Codeforces 847 C. Sum of Nestings (技巧)
- CodeForces 847CSum of Nestings
- Sum of Factorials
- Sum of Factorials
- Sum Of SubRectangular Parallelepiped
- HDU3022:Sum of Digits
- HDU3022:Sum of Digits
- lucky sum of digit
- 【模拟】Sacrament of Sum
- 【Hash】Sacrament of Sum
- Sum of Factorials
- hdu4432 Sum of divisors
- Game of Sum
- Sum of absolute differences
- denim1: sum of subsets
- Sum of a tree
- Sum of a tree
- 手把手教你搭建智能合约测试环境、开发、编译、部署以及如何通过JS调用合约方法
- 主席树查询区间第K大模板
- java数据库编程--使用元数据分析数据库
- Codeforces847B Preparing forMerge Sort
- 事件绑定和事件委托整理
- Codeforces847C Sum of Nestings
- 读Zepto源码之Touch模块
- Java算法中Integer常用方法
- c#类的相关知识
- ios-AFN中默认返回的数据转换成二进制数据
- 记录面试问题
- 搭建Windbg和Hyper-V第二代虚拟机,双机调试内核环境
- 图像质量评估算法-SAD SSD MAD MSD SSIM MS-SSIM
- Codeforces847D Dog Show