当前位置:网站首页>2021上海市赛-H-二分答案
2021上海市赛-H-二分答案
2022-07-25 15:23:00 【塔子哥来了】
题目大意:
一维坐标上有 n n n个车.每个车有坐标,速度(有方向),类型.问你最早在多少秒有车发生碰撞
n ≤ 1 e 5 n \leq 1e5 n≤1e5
题目思路:
显然二分答案。关键在于如何check相撞?
没有相撞等价于任意两对不同类型的车之间的相对位置没有发生变化.
这么check显然是对的,但是显然复杂度是爆炸的.
但我们只需要存一个车左边离他最近的不同类的车的下标以及右边离它最近的不同类的车的下标。
二分后sort,然后 O ( n ) O(n) O(n)的check每个车是否符合条件即可。
因为一旦发生相撞,一定有车的左右状态发生改变.
或者说:相对位置没发生改变等价于每个车左右最近的两个不同类的车没发生改变
#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;
}
边栏推荐
- How spark gets columns in dataframe --column, $, column, apply
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- The difference between Apple buy in and apple pay
- 期货在线开户是否安全?去哪家公司手续费最低?
- 为什么PrepareStatement性能更好更安全?
- Simulate setinterval timer with setTimeout
- 数据系统分区设计 - 分区再平衡(rebalancing)
- 如何更新更新数据库中的json值?
- Spark AQE
- JVM-动态字节码技术详解
猜你喜欢

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

ML - 自然语言处理 - 关键技术

理解“平均负载”

NPM's nexus private server e401 E500 error handling record

How to solve the problem of scanf compilation error in Visual Studio

为什么PrepareStatement性能更好更安全?

Image cropper example

Spark partition operators partitionby, coalesce, repartition

MySQL transactions and mvcc

获取键盘按下的键位对应ask码
随机推荐
Recommend 10 learning websites that can be called artifact
JVM parameter configuration details
2019陕西省省赛K-变种Dijstra
Understanding the execution order of T-SQL query from the execution order of join on and where
ML - Speech - advanced speech model
Notes on inputview and inputaccessoryview of uitextfield
Spark DF增加一列
BPSK调制系统MATLAB仿真实现(1)
CGO is realy Cool!
CGO is realy Cool!
HBCK fix problem
matlab randint,Matlab的randint函数用法「建议收藏」
Spark SQL null value, Nan judgment and processing
Endnote 无法编辑range 解决
IOS interview questions
Spark SQL common time functions
Spark获取DataFrame中列的方式--col,$,column,apply
Node learning
数据系统分区设计 - 请求路由
How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?