当前位置:网站首页>2022杭电多校第三场 K题 Taxi
2022杭电多校第三场 K题 Taxi
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
给定我们二维平面上的 n n n个点,我们从当前点 ( x , y ) (x, y) (x,y),前往第 i i i个点,会减少花费 m i n ( ( ∣ x − x i ∣ + ∣ y − y i ∣ ) , w i ) min((|x - x_i | + |y - y_i |), w_i) min((∣x−xi∣+∣y−yi∣),wi),给定我们 q q q个询问,每次给我们一个二维平面上的一个点,我们需要计算出最大的减少的花费。
题解
主要用到了一个神奇的转换,我也没遇到过,比赛的时候我队友告诉我的~
我们需要预处理出来所有点的四个最值,这样计算的时候就可以 O ( 1 ) O(1) O(1)的时间计算出来了。
我们根据每个点的 w w w值从小到大进行排序,然后二分标号,然后计算出来当前的最大距离 d d d,通过 d d d和 w w w进行判断,下一步去左边还是右边,需要注意的是我们需要在二分过程中记录最值。具体细节看代码,看代码会好理解一些。
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
const int maxn = 100010, inf = 1e18;
typedef struct node
{
int x, y, w;
bool operator < (const struct node &t) const{
return w < t.w;
}
}Node;
Node points[maxn];
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, q;
int sx, sy;
int res;
bool check(int mid)
{
int t = -inf;
int w = points[mid].w;
t = max(t, sx + sy + a[mid]);
t = max(t, sx - sy + b[mid]);
t = max(t, -sx + sy + c[mid]);
t = max(t, -sx - sy + d[mid]);
res = max(res, min(w, t));
return w <= t;
}
signed main()
{
IOS;
int t; cin >> t;
while(t --){
cin >> n >> q;
for(int i = 0; i < n; i ++){
int x, y, w; cin >> x >> y >> w;
points[i] = {
x, y, w};
}
sort(points, points + n);
a[n] = b[n] = c[n] = d[n] = -inf;
for(int i = n - 1; i >= 0; i --){
a[i] = max(a[i + 1], -points[i].x - points[i].y);
b[i] = max(b[i + 1], -points[i].x + points[i].y);
c[i] = max(c[i + 1], points[i].x - points[i].y);
d[i] = max(d[i + 1], points[i].x + points[i].y);
}
while(q --){
res = -inf;
cin >> sx >> sy;
int l = 0, r = n - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;;
}
check(l);
cout << res << endl;
}
}
return 0;
}
边栏推荐
- [LeetCode] Summary of Matrix Simulation Related Topics
- Getting started with 3D modeling for games, what modeling software can I choose?
- [230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
- 00、数组及字符串常用的 API(详细剖析)
- 2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
- jenkins发送邮件系统配置
- 软件测试面试题:网络七层协仪具体?
- 元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
- Huggingface入门篇 II (QA)
- Mysql_14 存储引擎
猜你喜欢

oracle创建表空间

统计单词(DAY 101)华中科技大学考研机试题

找不到DiscoveryClient类型的Bean

First, the basic concept of reptiles

Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions

【LeetCode】Summary of Two Pointer Problems

软件开发工具的技术要素

电赛必备技能___定时ADC+DMA+串口通信

【LeetCode】滑动窗口题解汇总

2022牛客暑期多校训练营5(BCDFGHK)
随机推荐
软件质量评估的通用模型
软件测试面试题:什么是软件测试?软件测试的目的与原则?
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
oracle创建用户以后的权限问题
leetcode:266. 回文全排列
Handwritten Distributed Configuration Center (1)
leetcode: 266. All Palindromic Permutations
软件测试面试题:一套完整的测试应该由哪些阶段组成?
2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
D - I Hate Non-integer Number (选数的计数dp
uinty lua 关于异步函数的终极思想
阅读笔记:如何理解DevOps?
矩阵数学原理
SQL association table update
STC89C52RC的P4口的应用问题
NMS原理及其代码实现
图解 Canvas 入门
典型相关分析CCA计算过程
leetcode经典例题——单词拆分
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions