跳到正文

题解

CF847C 题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年6月25日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 1 分钟
获赞
1
题目
CF847C

版本

  1. v1当前

思路

显然,如果一个括号里包含了 xx 个括号,里面的括号不管怎么排序,以这个括号为外层则一定会产生 xx 个嵌套。所以,我们只要对每个括号贪心判断能不能当外层即可。

代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n;
ll m;
void print (int n, ll m) {
	if (! n)
		return ;
	if (m < n - 1)
		print (n - 1, m), cout << "()";
	else
		cout << '(', print (n - 1, m - n + 1), cout << ')';
	return ;
}
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> n >> m;
	if (n * (n - 1ll) / 2 < m)
		cout << "Impossible";
	else
		print (n, m);
	return 0;
}
原文