Codeforces Round 946 (Div. 3) F. Cutting Game 题解 模拟

Cutting Game

题目描述

Alice and Bob were playing a game again. They have a grid of size a × b a \times b a×b ( 1 ≤ a , b ≤ 1 0 9 1 \le a, b \le 10^9 1a,b109), on which there are n n n chips, with at most one chip in each cell. The cell at the intersection of the x x x-th row and the y y y-th column has coordinates ( x , y ) (x, y) (x,y).

Alice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character ‘U’, ‘D’, ‘L’, or ‘R’ and an integer k k k:

  • If the character is ‘U’, then the first k k k remaining rows will be cut;
  • If the character is ‘D’, then the last k k k remaining rows will be cut;
  • If the character is ‘L’, then the first k k k remaining columns will be cut;
  • If the character is ‘R’, then the last k k k remaining columns will be cut.

Based on the initial state of the grid and the players’ moves, determine the number of points earned by Alice and Bob, respectively.

输入描述

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains four integers a a a, b b b, n n n, and m m m ( 2 ≤ a , b ≤ 1 0 9 2 \le a, b \le 10^9 2a,b109, 1 ≤ n , m ≤ 2 ⋅ 1 0 5 1 \le n, m \le 2 \cdot 10^5 1n,m2105) — the dimensions of the grid, the number of chips, and the number of moves.

Each of the next n n n lines contain two integers x i x_i xi and y i y_i yi ( 1 ≤ x i ≤ a 1 \le x_i \le a 1xia, 1 ≤ y i ≤ b 1 \le y_i \le b 1yib) — the coordinates of the chips. All pairs of coordinates are distinct.

Each of the next m m m lines contain a character c j c_j cj and an integer k j k_j kj — the description of the j j j-th move. It is guaranteed that k k k is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move.

It is guaranteed that the sum of the values of n n n across all test cases in the test does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105. It is guaranteed that the sum of the values of m m m across all test cases in the test does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出描述

For each test case, output two integers — the number of points earned by Alice and Bob, respectively.

样例输入 #1

6
4 4 3 2
4 1
3 3
2 4
D 2
R 1
4 4 3 3
4 1
3 2
2 3
D 1
L 1
U 2
3 5 3 2
1 3
2 2
3 3
R 2
R 2
6 4 4 2
1 4
2 3
5 3
1 1
R 1
U 1
9 3 2 1
6 1
3 3
D 8
10 10 2 5
7 5
9 1
R 1
L 2
D 1
U 4
D 1

样例输出 #1

2 1
2 0
0 3
1 1
2 0
0 1

原题

CF——传送门
洛谷——传送门

思路

将筹码的位置按横坐标和纵坐标分别排序(升序/降序)。对于横坐标排序后的数组,用两个指针维护其边界,每次棋手下棋后更新棋盘的边界,循环判断边界上的筹码是否已经脱离棋盘(即是否在新的棋盘的边界之外),若已脱离,为当前棋手计分,同时指针指向数组内部的下一个筹码。纵坐标同理。此外,需记录每个筹码是否已经参与计分,避免在横坐标与纵坐标上重复计分。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        int a, b, n, m;
        cin >> a >> b >> n >> m;
        vector<pair<int, int>> ver(n), hor(n); // 竖直上单调的数组,水平上单调的数组
        for (int i = 0; i < n; i++)
        {
            cin >> ver[i].first >> ver[i].second;
            hor[i] = ver[i];
        }
        auto hor_cmp = [](pair<int, int> a, pair<int, int> b)
        {
            return a.second < b.second;
        };
        sort(ver.begin(), ver.end());          // 按x值升序的坐标数组
        sort(hor.begin(), hor.end(), hor_cmp); // 按y值升序的坐标数组
        int ans_Alice = 0, ans_Bob = 0;
        map<pair<int, int>, bool> del;                     // 记录是否已经删除该坐标,避免水平和竖直删除时重复计数
        int up = 0, down = n - 1, left = 0, right = n - 1; // 当前在相应数组中的索引的上界,下界,左界,右界
        int a_up = 1, a_down = a, b_left = 1, b_right = b; // 当前网格的上界,下界,左界,右界
        auto add = [&](int x, int y, int i)
        {
            if (!del[{x, y}])
            {
                if (i & 1) // 奇数是Alice下的棋
                    ans_Alice++;
                else
                    ans_Bob++;
                del[{x, y}] = 1; // 记录已删除该筹码
            }
        };
        char c;
        int k;
        for (int i = 1; i <= m; i++)
        {
            cin >> c >> k;
            switch (c)
            {
            case 'U':
                a_up += k;                                 // 边界收缩
                while (up <= down && ver[up].first < a_up) // 用单调数组上的边界索引找到所有该步棋中被移除的筹码
                {
                    add(ver[up].first, ver[up].second, i); // 为棋手加分
                    up++;                                  // 删去边界筹码后双指针收缩
                }
                break;
            case 'D':
                a_down -= k;
                while (up <= down && ver[down].first > a_down)
                {
                    add(ver[down].first, ver[down].second, i);
                    down--;
                }
                break;
            case 'L':
                b_left += k;
                while (left <= right && hor[left].second < b_left)
                {
                    add(hor[left].first, hor[left].second, i);
                    left++;
                }
                break;
            case 'R':
                b_right -= k;
                while (left <= right && hor[right].second > b_right)
                {
                    add(hor[right].first, hor[right].second, i);
                    right--;
                }
                break;
            default:
                cout << "ERROR!" << '\n';
                break;
            }
        }
        cout << ans_Alice << ' ' << ans_Bob << '\n';
    }

    return 0;
}
  • 21
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值