Codeforces Round 920 (Div. 3) D、E题解

Codeforces Round 920 (Div. 3) D、E题解

原文链接

D. Very Different Array

E. Eat the Chip

D. Very Different Array 题解

解题思路

在这个问题中,我们使用了贪心算法来最大化Vasya数组和Petya数组之间的总差异 D D D

1. 排序

首先对Petya的数组 a i a_i ai和Vasya可选的数字集合 b i b_i bi进行排序,为双指针操作做准备。

2. 双指针策略

使用两个指针,分别指向 a i a_i ai的两端和 b i b_i bi的两端。

3. 贪心选择

  • 在每一步中,计算 a i a_i ai两端与 b i b_i bi两端的差的绝对值。
  • 选择四种可能差值中的最大值,并累加到结果变量ans中。
  • 相应地移动指针。

4. 重复以上步骤

直到 a i a_i ai中的所有元素都被处理完毕。

通过这个过程,我们每步都选择当前最大的差异,确保了 D D D的值尽可能大。最后输出的ans即为所求的最大总差异 D D D

代码实现

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

const int N = 2e5 + 10;
int a[N];
int b[N];

int main()
{
    int _;
    cin >> _;
    while (_--)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        sort(a, a + n);
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
        }
        sort(b, b + m);
        int l = 0, r = m - 1;
        int L = 0, R = n - 1;
        ll D = 0;
        while (L <= R)
        {
            int l1, l2;
            int r1, r2;
            l1 = abs(a[L] - b[l]);
            l2 = abs(a[L] - b[r]);
            r1 = abs(a[R] - b[l]);
            r2 = abs(a[R] - b[r]);
            if (max(l1, l2) > max(r1, r2))
            {
                if (l1 > l2)
                {
                    D += l1;
                    l++;
                }
                else
                {
                    D += l2;
                    r--;
                }
                L++;
            }
            else
            {
                if (r1 > r2)
                {
                    D += r1;
                    l++;
                }
                else
                {
                    D += r2;
                    r--;
                }
                R--;
            }
        }
        cout << D << endl;
    }
    return 0;
}

E. Eat the Chip

E. Eat the Chip 题解

解题思路

横向位置关系分析

  1. 初始横向距离: 首先计算Alice和Bob棋子在横向上的初始距离 d = ∣ y a − y b ∣ d = |y_a - y_b| d=yayb

  2. 横向移动可能性:

    • 在每一次移动中,玩家可以选择横向不动或者横向移动(左或右一格)。
    • 这意味着,如果两个棋子的横向距离是d,那么在下一次移动后,横向距离可能变为 d − 1 d-1 d1 d d d d + 1 d+1 d+1
  3. 考虑棋盘边界:

    • 需要注意的是,棋子的横向移动不能超出棋盘的边界。
    • 如果棋子已经在最左侧或最右侧列,则它不能继续向该方向移动。

如何利用横向位置关系预测游戏结果

  1. 奇偶性分析:

    • 观察两个棋子的行间距 H = x b − x a H = x_b - x_a H=xbxa。如果 H H H是奇数,Alice不败,反之Bob不败
  2. 如果 H H H是奇数:

    • 分析横向移动后,他们是否可能在同一列相遇。
    • 如果在Alice的某次移动后,两个棋子的横向距离可以变为0,则Alice获胜。
  3. 如果 H H H是偶数:

    • 需要分析在这一行相遇时,横向距离是否可以为1。
    • 如果可以,那么Bob有可能获胜,因为他可以在下一个回合将棋子移动到Alice棋子的位置。

代码实现

#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

const int N = 2e5 + 10;

int main()
{
    int _;
    cin >> _;
    while (_--)
    {
        int h, w;
        int x1, y1;
        int x2, y2;
        cin >> h >> w >> x1 >> y1 >> x2 >> y2;
        int H = x2 - x1;
        bool isDraw = false;
        int isAliceWin = false;
        if (x2 <= x1 || x1 == h || x2 == 1)
        {
            isDraw = true;
        }
        if (H % 2 == 1) //奇数
        {
            int d1 = H / 2 + 1;
            if ((y1 + 1 < y2 && y1 + d1 < w) || (y1 - 1 > y2 && y1 - d1 > 1))
            {
                isDraw = true;
            }
            else
            {
                isAliceWin = true;
            }
        }
        else
        {
            int d1 = H / 2;
            int d2 = H / 2;
            if ((y1 < y2 && y2 - d2 > 1) || (y1 > y2) && y2 + d2 < w)
            {
                isDraw = true;
            }
            else
            {
                isAliceWin = false;
            }
        }
        if (isDraw)
        {
            cout << "Draw" << endl;
        }
        else if (isAliceWin)
        {
            cout << "Alice" << endl;
        }
        else
        {
            cout << "Bob" << endl;
        }
    }
    return 0;
}
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值