Codeforces Round 996 (Div. 2)题解(ABC)

比赛链接:Codeforces Round 996 (Div. 2)

A-Two Frogs

思路:通过观察,考虑Alice的胜败与二者初始位置的距离有关,具体地,距离为偶数则Alice必胜,反之则败。

#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
using namespace std;

int t, n, a, b;

int main()
{
    IOS;
    cin >> t;
    while(t--)
    {
        cin >> n >> a >> b;
        if((a-b)%2==0)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

B-Crafting

思路:由于每合成一个材料,其他材料都将减少,我们不难发现,当有多个材料出现缺失时,无论如何都无法合成神器;当只有一个材料缺失时,如其他材料存在没有盈余的,也无法合成神器,如果都有盈余,只要缺失的数量小于等于盈余的最小值则必定能合成神器。另外,当没有材料缺失时,必定能合成神器。

#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
using namespace std;

int t, n;

int main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        cin >> n;
        vector<int> a(n + 1), b(n + 1);
        rep(i, 1, n)
        {
            cin >> a[i];
        }
        rep(i, 1, n)
        {
            cin >> b[i];
        }
        int cnt = 0;
        rep(i, 1, n)
        {
            if (b[i] - a[i] > 0)
                cnt++;//记录有几个存在缺失的材料
        }
        if (cnt > 1)
        {
            cout << "NO" << endl;
        }
        else if (cnt == 0)
        {
            cout << "YES" << endl;
        }
        else if (cnt == 1)
        {
            int minnd = INT_MAX;
            int pos = 0;
            rep(i, 1, n)
            {
                if (b[i] - a[i] > 0)
                    pos = i;//记录这唯一一个存在缺失的材料
                if (a[i] - b[i] >= 0)
                    minnd = min(minnd, a[i] - b[i]);//其他材料最小的盈余
            }
            if (b[pos] - a[pos] <= minnd)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
    }
    return 0;
}

C-The Trail

思路:根据题意,存在一个 x 满足 n*x=m*x,故使得 x 为 0 的方案必定正确。考虑到被篡改的路径只向右方或下方,即后操作的点不会影响之前的点,我们可以按照所给的方向字符串 s 逐个模拟:当方向为 D 时,修改该点,使其所在行总和为 0;当方向为 R 时,修改该点,使其所在列总和为 0;特殊地,对于终点 (n,m),我们根据最后的方向 s.back() 修改即可。记得开long long。

#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
using namespace std;

int t, n, m;
LL a[1003][1003];
string s;

void op()
{
    int x = 1, y = 1;
    rep(i, 0, s.size() - 1)
    {
        LL sum = 0;
        if (s[i] == 'D')
        {
            rep(j, 1, m)
                sum += a[x][j];
            a[x][y] = -sum;
            x++;
        }
        else if (s[i] == 'R')
        {
            rep(j, 1, n)
                sum += a[j][y];
            a[x][y] = -sum;
            y++;
        }
    }
    LL finsum = 0;//处理最后一个点
    if (s.back() == 'D')
    {
        rep(i, 1, m)
            finsum += a[n][i];
        a[n][m] = -finsum;
    }
    else if (s.back() == 'R')
    {
        rep(i, 1, n)
            finsum += a[i][m];
        a[n][m] = -finsum;
    }
}

int main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        rep(i,1,n)
        {
            rep(j, 1, m)
                a[i][j] = 0;
        }
        cin >> s;
        rep(i, 1, n)
        {
            rep(j, 1, m)
                cin >> a[i][j];
        }
        op();
        rep(i, 1, n)
        {
            rep(j, 1, m)
                cout << a[i][j] << " ";
            cout << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值