[ABC273D] LRUD Instructions 题解
1.AT_abc333_e [ABC333E] Takahashi Quest 题解2.[ABC265F] Manhattan Cafe 题解3.[ABC271E] Subsequence Path 题解
4.[ABC273D] LRUD Instructions 题解
5.P8085 [COCI2011-2012#4] KRIPTOGRAM 题解6.[ABC238F] Two Exams 题解7.[ABC217F] Make Pair 题解8.[ABC219F] Cleaning Robot 题解9.[ABC219E] Moat 题解10.[ABC221D] Online games 题解11.[ABC221E] LEQ 题解12.[ABC223E] Placing Rectangles 题解13.[ABC211D] Number of Shortest paths 题解14.[ABC211F] Rectilinear Polygons 题解15.[ABC223F] Parenthesis Checking 题解16.CF154C Double Profiles 题解17.[ABC208D] Shortest Path Queries 2 题解18.[ABC212E] Safety Journey 题解19.[ABC229E] Graph Destruction 题解20.[ABC240E] Ranges on Tree 题解21.[ABC261E] Many Operations 题解22.P10842 【MX-J2-T3】Piggy and Trees 题解[ABC273D] LRUD Instructions 题解
很好的一道大模拟,使我爆
思路解析
大模拟,我们只需要用一个
其中判断在当前移动方向上离当前点最近的点可以使用离散化,然后使用二分法查找第一个大于当前点的障碍物,而若是朝负方向移动,则可以修改 lower_bound
里的迭代器起终点和 cmp
函数,这样就可以用 lower_bound
实现反向二分。
PS:考试时能用 vector
就千万不要用 set
!!!常数奇大无比!! 挂分记录
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fir first
#define sec second
int h, w, n;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int sx, sy;
map<int, vector<int> > mph, mpl;
map<char, int> mpc;
int q;
void init() {
mpc['U'] = 0;
mpc['D'] = 1;
mpc['L'] = 2;
mpc['R'] = 3;
}
signed main() {
init();
cin >> h >> w >> sx >> sy;
cin >> n;
for(int i = 1, r, c; i <= n; i++) {
cin >> r >> c;
mph[r].push_back(c);
mpl[c].push_back(r);
}
for(auto &it : mph) {
it.sec.push_back(0);
it.sec.push_back(2e9);
sort(it.sec.begin(), it.sec.end());
}
for(auto &it : mpl) {
it.sec.push_back(0);
it.sec.push_back(2e9);
sort(it.sec.begin(), it.sec.end());
}
cin >> q;
int x = sx, y = sy;
while(q--) {
char ch;
int l;
cin >> ch >> l;
int d = mpc[ch];
int nx = x + dx[d] * l;
nx = max(1ll, nx);
nx = min(nx, h);
int ny = y + dy[d] * l;
ny = max(1ll, ny);
ny = min(ny, w);
if(d == 0) {
if(!mpl[ny].empty()) {
int temp = *lower_bound(mpl[ny].rbegin(), mpl[ny].rend(), x, [](int xx, int yy) { //lambda表达式
return xx > yy;
});
if(temp >= nx && temp <= x) {
x = temp + 1;
}
else {
x = nx;
}
}
else {
x = nx;
}
}
else if(d == 1) {
if(!mpl[ny].empty()) {
int temp = *lower_bound(mpl[ny].begin(), mpl[ny].end(), x);
if(temp >= x && temp <= nx) {
x = temp - 1;
}
else {
x = nx;
}
}
else {
x = nx;
}
}
else if(d == 2) {
if(!mph[nx].empty()) {
int temp = *lower_bound(mph[nx].rbegin(), mph[nx].rend(), y, [](int xx, int yy) {
return xx > yy;
});
if(temp >= ny && temp <= y) {
y = temp + 1;
}
else {
y = ny;
}
}
else {
y = ny;
}
}
else if(d == 3) {
if(!mph[nx].empty()) {
int temp = *lower_bound(mph[nx].begin(), mph[nx].end(), y);
if(temp >= y && temp <= ny) {
y = temp - 1;
}
else {
y = ny;
}
}
else {
y = ny;
}
}
cout << x << " " << y << "\n";
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】