Codeforces Round #626(Div.2) 解题报告

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

【前言】

最近沉迷只狼,好久没有打CF了,水平下降了不少,昨天打完修罗结局今天搞了一个Div2练练手,没想到是真的菜。。。(虽然我一直都挺菜),看来还是得经常做题啊

【A.Even Subset Sum Problem】

【题目大意】
给你一个序列,让你输出一个和为偶数的子序列,找不到则输出-1

【解题思路】
随便写写

【AC代码】

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		int a = -1, b = -1, ans = -1;
		for (int i = 1; i <= n; ++i) {
			int x;
			cin >> x;
			if (x % 2 == 0) ans = i;
			else if (a == -1) a = i;
			else if(b == -1) b = i;
		}
		if (ans != -1) cout << "1\n" << ans << '\n';
		else if (a != -1 && b != -1) cout << "2\n" << a << " " << b << '\n';
		else cout << "-1\n";
	}
	return 0;
}

【B.Count Subrectangles】

【题目大意】
给你两个由0和1组成的一维数组a和b,定义c[i][j] = a[i] * b[j],问你c数组中子数组大小为k,且全为1的数组个数是多少

【解题思路】
考虑模拟矩阵乘法,假设c中有一个满足条件的大小为n * m的子数组,那么a中至少有一个长度为n,b中长度为m的连续1序列(可以画图理解一下)

那么我们可以sqrt(k)的时间筛出k中的所有因子,大概有logk个,然后对于每一个因子p我们去a和b中找到长度为p和k/p的连续1序列,这样总时间O(nlogn)可以通过

注意要开longlong

【AC代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= m; ++i) cin >> b[i];
	vector<int> v;
	int kk = sqrt(k);
	for (int i = 1; i <= kk; ++i) {
		if (k % i == 0) {
			v.push_back(i);
			if (k / i != i) v.push_back(k / i);
		}
	}
	int l = v.size();
	ll ans = 0;
	for (int i = 0; i < l; ++i) {
		ll x = 0, y = 0, cnt = 0;
		for (int j = 1; j <= n; ++j) {
			if (a[j]) ++cnt;
			else cnt = 0;
			if (cnt == v[i]) ++x, --cnt;
		}
		cnt = 0;
		for (int j = 1; j <= m; ++j) {
			if (b[j]) ++cnt;
			else cnt = 0;
			if (cnt == k / v[i]) ++y, --cnt;
		}
		ans += x * y;
	}
	cout << ans << '\n';
	return 0;
}

【C.Unusual Competitions】

【题目大意】
给你一个有 ‘(’ 和 ‘)‘ 组成的字符串,你可以花费n时间将长度为n的子序列重排列,问你最少花多长时间可以将该字符串重排列为合法字符串(即 ’(‘ 和 ‘)’ 一一对应),如果无法重排列则输出-1

【解题思路】
乍一看以为是一道dp,其实就是一个模拟题

首先很容易想到左括号和右括号数目不等的时候是无法重排列的

然后对于s[i]:

  1. s[i] == ‘(’
    如果前面有 ‘)’,那么当左括号数目l和右括号数目r相等时,对于答案的贡献为l * 2,比如 “) ) ( (”,然后将其从字符串中去除
  2. s[i] == ‘)’
    如果此时前面全部都为 ’(‘ ,那么由于该字符串是可以重排列的,那么其后面一定会有与前面的 ‘(’ 相对应的 ‘)’ ,则其对应的"( )"对答案贡献为0,可以直接从字符串中去除
    否则如果前面有 ‘)’ ,由于当左括号数目等于右括号时我们会将其消去,那么此时 ’(‘ 数目必定小于 ‘)’,那么后面一定有对应的 ‘)’ ,我们只要等待两个数目相等是将其消去即可

消去字符的步骤我们可以用栈来实现

【AC代码】

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	int a = 0, b = 0;
	for (int i = 0; i < n; ++i) {
		if (s[i] == '(') ++a;
		else ++b;
	}
	if (a != b) {
		cout << "-1\n";
		return 0;
	}
	stack<char> q;
	int l = 0, r = 0, ans = 0;
	for (int i = 0; i < n; ++i) {
		if (s[i] == '(') {
			q.push(s[i]);
			++l;
			if (l == r) {
				ans += l * 2;
				l = r = 0;
				while (!q.empty()) q.pop();
			}
		}
		else {
			if (!q.empty() && q.size() == l && q.top() == '(') q.pop(), --l;
			else {
				q.push(s[i]);
				++r;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值