当前位置:网站首页>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;
}
边栏推荐
- wait()和sleep()的区别理解
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
- Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
- JVM dynamic bytecode technology details
- CF685B-求有根树每颗子树的重心
- How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
- window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
- Iframe nested other website page full screen settings
- 获取键盘按下的键位对应ask码
- JVM-参数配置详解
猜你喜欢

ML - Speech - advanced speech model

PAT甲级1152 Google Recruitment (20 分)

BPSK调制系统MATLAB仿真实现(1)

Get the ask code corresponding to the key pressed by the keyboard

Solve the timeout of dbeaver SQL client connection Phoenix query

Reflection - Notes

Spark SQL null value, Nan judgment and processing

Spark partition operators partitionby, coalesce, repartition

Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)

分布式原理 - 什么是分布式系统
随机推荐
数据系统分区设计 - 分区再平衡(rebalancing)
C#精挑整理知识要点9 集合2(建议收藏)
Spark AQE
JVM知识脑图分享
2019陕西省省赛K-变种Dijstra
JVM knowledge brain map sharing
分布式原理 - 什么是分布式系统
Instance tunnel use
Spark SQL null value, Nan judgment and processing
ios 面试题
How to solve the login problem after the 30 day experience period of visual stuido2019
PAT甲级1152 Google Recruitment (20 分)
2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
MySQL installation and configuration super detailed tutorial and simple database and table building method
How to finally generate a file from saveastextfile in spark
ML - Speech - advanced speech model
ML - 自然语言处理 - 自然语言处理简介
ML - 自然语言处理 - 基础知识
解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
<栈模拟递归>