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;
}
### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值