AtCoder Beginner Contest 243 C - Collision 2

题意

题目
给n个点,每个点有移动方向(向左或向右),判断是否会有碰撞

思路

unordered_map<int, vector<PII>> y坐标相同的存在vecctor中,vector中存x的坐标和移动方向。
枚举每个y,再枚举y中的每个x,选出往右走的最左边的点,和往左走的最右边的点,如果有肯定会碰撞

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;

int main()
{
    int n;
    cin >> n;
    
    vector<PII> p(n + 10);
    string s;
    unordered_map<int, vector<PII>> h;
    for(int i = 0; i < n; i ++ )
        scanf("%d%d", &p[i].x, &p[i].y);
        
    cin >> s;
    
    
    for(int i = 0; i < n; i ++ )
        h[p[i].y].push_back({p[i].x, s[i] == 'R'? 1 : 0});
    
    bool flag = false;
    for(auto [a, b] : h)
    {
        int r = 1e9, l = -1e9;
        for(auto [c, d] : b)
        {
            if(d == 1)  r = min(r, c);
            else    l = max(l, c);
        }
        if(r < l)
        {
            flag = true;
            break;
        }
    }
    if(flag)    puts("Yes");
    else    puts("No");
    return 0;
}
posted @ 2022-03-13 15:14  inss!w!  阅读(51)  评论(0编辑  收藏  举报