Codeforces Round 956 (Div. 2) 部分题解A~C

A. Array Divisibility

题目大意

构造长度为n的数组,满足:

  • 所有j的aj之和可以被k整除,其中j是k的倍数,k的取值为1~n。

思路

构造序列1->n即可满足条件。

代码实现
void solve() {
    ll n; cin >> n;
    for (int i = 1; i <= n; i++)cout << i << " ";
    cout << "\n";
}

B. Corner Twist

题目大意

给定a和b两个网格,网格中只存在0、1、2三种值。

给定操作,可以选择一个矩形的一条对角线上的两个端点,让他们加1/2;另一条对角线的端点值加2/1,然后对这些值取模3。问是否存在操作可以让a和b两个网格一致。

思路

考虑网格的边上的值,边上的值的变化会受到其他的影响,所以我们优先去找如何让边上的值相等即可。

正扫一遍,反扫一遍,然后比较数组即可。

代码实现
void solve() {
    ll n, m; cin >> n >> m;
    vector<vector<char>>a(n + 1, vector<char>(m + 1));
    vector<vector<char>>b(n + 1, vector<char>(m + 1));
    for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> a[i][j];
    for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> b[i][j];
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            if (a[i][j] != b[i][j]) {
                if (a[i][j] == '0') {
                    if (b[i][j] == '1') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 1;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 2;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 2;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j + 1] = '1';
                    }else if(b[i][j]=='2') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 2;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 1;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 1;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j + 1] = '1';
                    }
                }
                else if (a[i][j] == '1') {
                    if (b[i][j] == '2') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 1;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 2;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 2;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j + 1] = '1';
                    }
                    else if (b[i][j] == '0') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 2;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 1;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 1;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j + 1] = '1';
                    }
                }
                else {
                    if (b[i][j] == '0') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 1;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 2;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 2;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j+1] = '1';
                    }
                    else if (b[i][j] == '1') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i + 1][j + 1] += 2;
                        if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
                        if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
 
                        a[i + 1][j] += 1;
                        if (a[i + 1][j] == '3')a[i + 1][j] = '0';
                        if (a[i + 1][j] == '4')a[i + 1][j] = '1';
 
                        a[i][j + 1] += 1;
                        if (a[i][j + 1] == '3')a[i][j + 1] = '0';
                        if (a[i][j + 1] == '4')a[i][j + 1] = '1';
                    }
                }
            }
        }
    }
    for (int i = n; i > 1; i--) {
        for (int j = m; j > 1; j--) {
            if (a[i][j] != b[i][j]) {
                if (a[i][j] == '0') {
                    if (b[i][j] == '1') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 1;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
 
                        a[i - 1][j] += 2;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 2;
                        if (a[i][j - 1] == '3')a[i][j - 1] = '0';
                        if (a[i][j - 1] == '4')a[i][j - 1] = '1';
                    }
                    else if (b[i][j] == '2') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 2;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j -1] = '1';
 
                        a[i - 1][j] += 1;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 1;
                        if (a[i][j - 1] == '3')a[i][j - 1] = '0';
                        if (a[i][j - 1] == '4')a[i][j - 1] = '1';
                    }
                }
                else if (a[i][j] == '1') {
                    if (b[i][j] == '2') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 1;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
 
                        a[i - 1][j] += 2;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 2;
                        if (a[i][j - 1] == '3')a[i - 1][j] = '0';
                        if (a[i][j - 1] == '4')a[i - 1][j] = '1';
                    }
                    else if (b[i][j] == '0') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 2;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
 
                        a[i - 1][j] += 1;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 1;
                        if (a[i][j - 1] == '3')a[i][j - 1] = '0';
                        if (a[i][j - 1] == '4')a[i][j - 1] = '1';
                    }
                }
                else {
                    if (b[i][j] == '0') {
                        a[i][j] += 1;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 1;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
 
                        a[i - 1][j] += 2;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 2;
                        if (a[i][j - 1] == '3')a[i][j - 1] = '0';
                        if (a[i][j - 1] == '4')a[i][j-1] = '1';
                    }
                    else if (b[i][j] == '1') {
                        a[i][j] += 2;
                        if (a[i][j] == '3')a[i][j] = '0';
                        if (a[i][j] == '4')a[i][j] = '1';
 
                        a[i - 1][j - 1] += 2;
                        if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
                        if (a[i - 1][j - 1] == '4')a[i - 1][j-1] = '1';
 
                        a[i - 1][j] += 1;
                        if (a[i - 1][j] == '3')a[i - 1][j] = '0';
                        if (a[i - 1][j] == '4')a[i - 1][j] = '1';
 
                        a[i][j - 1] += 1;
                        if (a[i][j - 1] == '3')a[i][j - 1] = '0';
                        if (a[i][j - 1] == '4')a[i][j-1] = '1';
                    }
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] != b[i][j]) {
                cout << "NO\n"; return;
            }
        }
    }
    cout << "YES\n";
}

C. Have Your Cake and Eat It Too

题目大意

三个人吃蛋糕,每个人对蛋糕不同位置有自己的估价。

问如何分配蛋糕,使三个人获得的蛋糕价值均高于蛋糕价值和的三分之一。

思路

分类讨论:三个人拿蛋糕的顺序

  • abc

  • acb

  • bac

  • bca

  • cab

  • cba

代码实现
ll a[2100000];
ll b[2100000];
ll c[2100000];
void solve() {
    ll n; cin >> n;
    ll need = 0;
    a[0] = b[0] = c[0] = 0;
    for (int i = 1; i <= n; i++)cin >> a[i], need += a[i];
    for (int i = 1; i <= n; i++)cin >> b[i];
    for (int i = 1; i <= n; i++)cin >> c[i];
    need = ceil(need / 3.0);
    for (int i = 1; i <= n; i++)a[i] += a[i - 1];
    for (int i = 1; i <= n; i++)b[i] += b[i - 1];
    for (int i = 1; i <= n; i++)c[i] += c[i - 1];
    a[n + 1] = b[n + 1] = c[n + 1] = 1e22;
    //abc
    ll id = lower_bound(a + 1, a + 1 + n, need) - a;
    ll id2 = lower_bound(b + 1, b + 1 + n, b[id] + need) - b;
    if (c[n] - c[id2] >= need) {
        cout << 1 << " " << id << " " << id + 1 << " " << id2 << " " << id2 + 1 << " " << n << "\n";
        return;
    }
    //acb
    id = lower_bound(a + 1, a + 1 + n, need) - a;
    id2 = lower_bound(c + 1, c + 1 + n, c[id] + need) - c;
    if (b[n] - b[id2] >= need) {
        cout << 1 << " " << id << " " << id2 + 1 << " " << n << " " << id + 1 << " " << id2 << "\n";
        return;
    }
    //bca
    id = lower_bound(b + 1, b + 1 + n, need) - b;
    id2 = lower_bound(c + 1, c + 1 + n, c[id] + need) - c;
    if (a[n] - a[id2] >= need) {
        cout << id2 + 1 << " " << n << " " << 1 << " " << id << " " << id + 1 << " " << id2 << "\n";
        return;
    }
    //bac
    id = lower_bound(b + 1, b + 1 + n, need) - b;
    id2 = lower_bound(a + 1, a + 1 + n, a[id] + need) - a;
    if (c[n] - c[id2] >= need) {
        cout << id + 1 << " " << id2 << " " << 1 << " " << id << " " << id2 + 1 << " " << n << "\n";
        return;
    }
    //cab
    id = lower_bound(c + 1, c + 1 + n, need) - c;
    id2 = lower_bound(a + 1, a + 1 + n, a[id] + need) - a;
    if (b[n] - b[id2] >= need) {
        cout << id + 1 << " " << id2 << " " << id2 + 1 << " " << n <<" " << 1 << " " << id << "\n";
        return;
    }
    //cba
    id = lower_bound(c + 1, c + 1 + n, need) - c;
    id2 = lower_bound(b + 1, b + 1 + n, b[id] + need) - b;
    if (a[n] - a[id2] >= need) {
        cout << id2 + 1 << " " << n << " " << id + 1 << " " << id2 << " " << 1 << " " << id << "\n";
        return;
    }
    cout << "-1\n";
}

  • 21
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值