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;
}