AtCoder Beginner Contest 243 C - Collision 2
阿新 • • 發佈:2022-03-13
題意
題目
給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; }