其他分享
首页 > 其他分享> > Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

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

作者:互联网

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

比赛链接

A. Even Subset Sum Problem

找出第一次出现的一个偶数或者两个奇数

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define MEM(X,Y) memset(X,Y,sizeof(X));
#define OPEN freopen("abc.in","r",stdin);
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
using namespace std;
 
int main(void)
{
	//OPEN;
	int ji[2],ou;
	int T,n,a;
	int cnt = 0;
	bool flag;
	scanf("%d", &T);
	while (T--)
	{
		flag = 0;
		cnt = 0;
		scanf("%d", &n);
		ou = -1;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a);
			if (!flag)
			{
				if (a & 01)
					ji[cnt++] = i;
				else
					ou = i;
				if (cnt == 2 || ou != -1)
					flag = 1;
			}
		}
		if (flag)
		{
			if (ou != -1)
				printf("1\n%d", ou);
			else
				printf("2\n%d %d", ji[0], ji[1]);
		}
		else
			printf("-1");
		if (T)
			printf("\n");
	}
 
	return 0;
}

B. Count Subrectangles

找出a和b数组中1的连续子序列不同长度的个数,然后输出在1 ~ n,1 ~ m之间乘积为k时,对应的c[i],d[j]的乘积和

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define MEM(X,Y) memset(X,Y,sizeof(X));
#define OPEN freopen("abc.in","r",stdin);
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
using namespace std;
 
bool a[40010], b[40010];
int c[40010], d[40010];
int cnt;
int result;
 
int main(void)
{
 
	int n, m, k, t;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &t);
		a[i] = t;
	}
	for (int i = 0; i < m; ++i)
	{
		scanf("%d", &t);
		b[i] = t;
	}
	cnt = 0;
	for (int i = 0; i < n; ++i)
		if (a[i])
		{
			++cnt;
			for (int j = 1; j <= cnt; ++j)
				++c[j];
		}
		else
			cnt = 0;
	cnt = 0;
	for (int i = 0; i < m; ++i)
		if (b[i])
		{
			++cnt;
			for (int j = 1; j <= cnt; ++j)
				++d[j];
		}
		else
			cnt = 0;
	long long result = 0;
	for (int i = 1; i <= min(k, n); ++i)
	{
		if (k % i == 0 && k / i <= m)
			result += (long long)c[i] * d[k / i];
	}
	printf("%lld", result);
 
	return 0;
}

C. Unusual Competitions

如果寻找到已有的左括号数量等于右括号数量就停止,从有括号的位置开始回溯去寻找已摆放正确的括号的长度,用当前位置减去正确摆放的长度再减去上一次从左右括号数量相等的位置去记录,并把左右括号的数量归零,最终如果左括号的值与右括号的数量不相等,输出-1,否则输出记录和

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define MEM(X,Y) memset(X,Y,sizeof(X));
#define OPEN freopen("abc.in","r",stdin);
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
 
std::stack<char> s;
 
int left, right;
char orin[1000005];
 
int main(void)
{
 
	int n;
	int o = 0, c = 0;
	int last = -1;
	long long  result = 0;
	scanf("%d", &n);
	scanf("%s", orin);
	for (int i = 0; i < n; ++i)
	{
		if (orin[i] == '(')
			++left;
		else
			++right;
		if (left == right)
		{
			int j = 0;
			for (; i - j > last; ++j)
			{
				if (orin[i - j] == ')')
					s.push(')');
				if (s.empty() && orin[i - j] == '(')
					break;
				if (!s.empty() && orin[i - j] == '(')
					s.pop();
			}
			result += (long long)i - j - last;
			last = i;
			right = left = 0;
		}
	}
	if (left != right)
		printf("-1");
	else
		printf("%lld", result);
 
	return 0;
}

D E F太可怕!!!

标签:based,626,括号,int,Moscow,++,orin,scanf,define
来源: https://blog.csdn.net/qq_45961715/article/details/104720126