A. Milica and String
Milica每次可以选择将字符串的前m位置一一换为字符C(A或B)。显然,对于每个字符串,至多一次(可能零次)操作便能实现条件。
- 若字符串中B的数量x小于所需y,将前(y-x)个A转化为B
- 若字符串中B的数量x大于所需y,将前(x-y)个B转化为A
void solve()
{
int n, m;
cin >> n >> m;
string s;
cin >> s;
int x = 0, y = 0;
for (auto u : s)
{
if (u == 'A')
x++;
else
y++;
}
if (y < m)
{
int i = 0;
while (y != m)
{
if (s[i] == 'A')
y++;
i++;
}
cout << 1 << '\n';
cout << i << ' ' << "B\n";
}
else if (y > m)
{
int i = 0;
while (y != m)
{
if (s[i] == 'B')
y--;
i++;
}
cout << 1 << '\n';
cout << i << ' ' << "A\n";
}
else
cout << 0 << '\n';
}
B. Milena and Admirer
从后往前倒推,对于存在ai>ai+1的情况,将ai分割成(向上取整)(设为p)份,并计算需要进行操作的次数,同时将当前ai的值置为ai/p
inline int tim(int n)
{
int m = 0, p = 1;
while (1)
{
if (p * 2 < n)
{
m += p;
p *= 2;
}
else
{
m += n - p;
break;
}
}
return m;
}
void solve()
{
int n;
cin >> n;
vector<int> a;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
a.push_back(x);
}
int pre = a[n - 1];
int ans = 0;
for (int i = n - 2; i >= 0; i--)
{
if (a[i] > pre)
{
int q = a[i] / pre;
if (a[i] % pre != 0)
q++;
ans += tim(q);
pre = a[i] / q;
}
else
pre = a[i];
}
cout << ans << '\n';
}
C. Colorful Grid
Elena能从起点走到终点,需要满足
- k>=(m+n-2)
- (k - (m + n - 2)) % 2 == 0 若为奇数,是无论如何也实现不了最终在终点位置停止的
可以分成两种方法,黑色方块颜色随意
- (k - m - n - 2) % 4 == 0 在走完初始的起点-终点路线后,可通过重复走右下角的一个小方格,实现目标
- (k - m - n - 2) % 4 == 2 在走完初始的起点-终点路线后(最后一步改为绕该方块的另外三条边),可通过重复走右下角的一个小方格,实现目标
void solve()
{
int n, m, k;
cin >> n >> m >> k;
if (k < (m + n - 2) || (k - (m + n - 2)) % 2 != 0)
{
cout << "NO\n";
return;
}
cout << "YES\n";
vector<vector<char>>a(n), b(n - 1);
for (int i = 0; i < n; i++)
a[i].resize(m - 1, 'R');
for (int i = 0; i < n - 1; i++)
b[i].resize(m, 'R');
char co[2] = { 'R','B' };
int f = 0;
for (int i = 0; i < n - 1; i++)
{
b[i][0] = co[f];
f = 1 - f;
}
for (int i = 0; i < m - 1; i++)
{
a[n-1][i] = co[f];
f = 1 - f;
}
if ((k - m - n - 2) % 4 == 0)
{
a[n - 2][m - 2] = a[n - 1][m - 2];
b[n - 2][m - 1] = b[n - 2][m - 2] = co[f];
}
else
{
a[n - 2][m - 2] = a[n - 1][m - 2] = co[f];
b[n - 2][m - 1] = b[n - 2][m - 2] = co[1 - f];
}
for (auto u : a)
{
for (auto v : u)
cout << v << ' ';
cout << '\n';
}
for (auto u : b)
{
for (auto v : u)
cout << v << ' ';
cout << '\n';
}
}
D. Absolute Beauty
将所有ai,bi的组合视为一条线段,只有当满足第一种情况时,绝对值的和才会增加。
转化为求不重合线段中,右侧左端点和左侧右端点的最长距离。
进一步化简,即为求所有线段中最左侧线段的右端点和最右侧线段左端点之间的距离,若为0或负数,则不进行交换操作
struct point
{
int x, y;
};
void solve()
{
int n;
cin >> n;
vector<point>a(n);
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
a[i].x = x;
}
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
a[i].y = x;
}
int l = 1e10, r = 0;
int sum = 0;
for (auto u : a)
{
if (u.x < u.y)
{
r = max(r, u.x);
l = min(l, u.y);
}
else
{
r = max(r, u.y);
l = min(l, u.x);
}
sum += abs(u.x - u.y);
}
if (l < r)
sum += 2 * (r - l);
cout << sum << '\n';
}