Codeforces Round #547 (Div. 3)

A. Game 23

http://codeforces.com/contest/1141/problem/A

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int main()
{
	int n, m;
	cin >> n >> m;
	if (m == n)
	{
		cout << 0 << endl;
		return 0;
	}
	int ans = 0;
	if (m%n == 0)
	{
		int x = m / n;
		while (x % 2 == 0)
		{
			x /= 2;
			ans++;
		}
		while (x % 3 == 0)
		{
			x /= 3;
			ans++;
		}
		if (x == 1)
		{
			cout << ans << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}

 

B. Maximal Continuous Rest

http://codeforces.com/contest/1141/problem/B

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int n, a[200010];
int main()
{
	int cnt = 0, max = 0;
	cin >> n;
	int flag = 0;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	if (n == 1 && a[1] == 1)
	{
		cout << 1 << endl;
		return 0;
	}
	if (n == 1 && a[1] == 0)
	{
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	for ( i = 1; i <= n; i++)
	{
		if (a[i] == 1 )
		{
			cnt = 1;
			int j = 1;
			for ( j = i+1; a[j] == 1; j++)
			{
				if (a[j] == 1)
					cnt++;
				else
					break;
			}
			i = j;
			if (j - 1 == n&&a[j - 1] == 1 )
			{
				for (int k = 1; a[k] == 1; k++)
				{
					if (a[k] == 1)
						cnt++;
				}
			}
			if (cnt > max)
				max = cnt;
		}
	}
	cout << max << endl;
	return 0;
}

C. Polycarp Restores Permutation

http://codeforces.com/contest/1141/problem/C

题解:设所求数组b第一位为0,用已知数组a加和可得b,若b中 最大值-最小值=n-1 且 没有重复元素,那b中每个元素-最小值+1,所得数组即为答案。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int n, a[200010] = { 0 };
int b[200010] = { 0 };
int c[200010];
int main()
{
	cin >> n;
	for (int i = 1; i <= n - 1; i++)
		cin >> a[i];
	int max = -inf, min = inf;
	for (int i = 1; i <= n - 1; i++)
		b[i + 1] = b[i] + a[i];
	for (int i = 1; i <= n; i++)
	{
		c[i] = b[i];
		if (b[i] > max)
			max = b[i];
		if (b[i] < min)
			min = b[i];
	}

	sort(c + 1, c + n + 1);
	int flag = 0;
	for (int i = 1; i < n; i++)
	{
		if (c[i] == c[i + 1])
		{
			flag = 1;
			break;
		}
	}
	if (max - min + 1 != n || flag == 1)
	{
		cout << -1 << endl;
		return 0;
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			if (i == n)
				cout << b[i] - min + 1 << endl;
			else
				cout << b[i] - min + 1 << ' ';
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值