当前位置:网站首页>2021 Shanghai match-h-two point answer
2021 Shanghai match-h-two point answer
2022-07-25 15:33:00 【Brother Tazi is here】
The main idea of the topic :
There is n n n A car . Each car has coordinates , Speed ( There is a direction ), type . Ask you the earliest time when a car collides
n ≤ 1 e 5 n \leq 1e5 n≤1e5
Topic ideas :
Obviously, the two-point answer . The key is how to check Collision ?
No collision is equivalent to The relative position between any two pairs of different types of cars Nothing has changed .
such check Obviously right , But obviously the complexity is explosive .
But we only need to save the subscript of different types of cars closest to it on the left and the subscript of different types of cars closest to it on the right .
Two minutes later sort, then O ( n ) O(n) O(n) Of check Whether each car meets the conditions .
Because once there is a collision , There must be a change in the left and right states of the car .
Or say : The relative position has not changed, which is equivalent to that the nearest two different cars around each car have not changed
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
struct Node
{
ll p , v , t , id;
bool operator < (const Node & a){
return p < a.p;
}
}a[maxn] , b[maxn] , c[maxn];
int L[maxn] , R[maxn];
int main()
{
ios::sync_with_stdio(false);
int n , k; cin >> n >> k;
for (int i = 1 ; i <= n ; i++){
cin >> a[i].p >> a[i].v >> a[i].t;
a[i].id = i;
}
sort(a + 1 , a + 1 + n);
R[a[n].id] = -1;
for (int i = n - 1 ; i >= 1 ; i--){
if (a[i].t == a[i + 1].t){
R[a[i].id] = R[a[i + 1].id];
}else {
R[a[i].id] = a[i + 1].id;
}
}
L[a[1].id] = -1;
for (int i = 2 ; i <= n ; i++){
if (a[i].t == a[i - 1].t){
L[a[i].id] = L[a[i - 1].id];
}else {
L[a[i].id] = a[i - 1].id;
}
}
ll l = 1 , r = 3e9;
// ll l = 1 , r = 1;
while (l <= r){
ll mid = l + r >> 1;
for (int i = 1 ; i <= n ; i++){
b[i] = a[i];
b[i].p += mid * b[i].v;
}
sort(b + 1 , b + 1 + n);
for (int i = 1 ; i <= n ; i++) c[b[i].id] = b[i];
bool ok = true;
for (int i = 1 ; i <= n ; i++){
int pos = b[i].id;
if (L[pos] != -1){
if (c[L[pos]].p >= c[pos].p){
ok = false;
break;
}
}
if (R[pos] != -1){
if (c[R[pos]].p <= c[pos].p){
ok = false;
break;
}
}
}
if (ok) l = mid + 1;
else r = mid - 1;
}
if (r == 3e9) cout << -1 << endl;
else cout << r << endl;
return 0;
}
边栏推荐
- Local cache --ehcache
- PAT甲级1152 Google Recruitment (20 分)
- Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
- The difference between Apple buy in and apple pay
- 解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
- 数据系统分区设计 - 请求路由
- 理解“平均负载”
- ML - 自然语言处理 - 基础知识
- PAT甲级1153 Decode Registration Card of PAT (25 分)
- Word 样式模板复制到另一文档
猜你喜欢

GAMES101复习:变换

Understanding the execution order of T-SQL query from the execution order of join on and where

获取键盘按下的键位对应ask码

ML - Speech - advanced speech model

Yan required executor memory is above the max threshold (8192mb) of this cluster!

理解“平均负载”

使用cpolar建立一个商业网站(如何购买域名)

Spark partition operators partitionby, coalesce, repartition

GAMES101复习:三维变换

ML - natural language processing - Introduction to natural language processing
随机推荐
MATLAB读取显示图像时数据格式转换原因
带你详细认识JS基础语法(建议收藏)
How spark gets columns in dataframe --column, $, column, apply
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
JVM parameter configuration details
Week303 of leetcode
2021上海市赛-H-二分答案
How to solve the login problem after the 30 day experience period of visual stuido2019
Solve the timeout of dbeaver SQL client connection Phoenix query
ML - 自然语言处理 - 自然语言处理简介
Spark judges that DF is empty
JVM-参数配置详解
MySQL transactions and mvcc
自定义注解校验API参数电话号
Are you ready to break away from the "involution circle"?
C # fine sorting knowledge points 10 generic (recommended Collection)
<栈模拟递归>
ML - natural language processing - Basics
C#精挑整理知识要点12 异常处理(建议收藏)
CF750F1-思维dp