CodeForces 699A Launch of Collider
2016-07-20 18:00
239 查看
题目地址
A. Launch of Collider
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles
will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is
the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even
integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle
will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave
it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) —
the number of particles.
The second line contains n symbols "L"
and "R". If the i-th symbol equals "L",
then the i-th particle will move to the left, otherwise the i-th
symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn(0 ≤ xi ≤ 109) —
the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
input
output
input
output
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will
simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
题意:直线上有一些点,给定点的初始位置及运动方向(坐标都为偶数),点都以固定速度1运动,求最早哪一时刻会有两个点相遇。
思路:只要考虑相邻的点即可,如果相邻的两个点左边在向右运动,右半在向左运动,那么算出相遇时间,与最小值进行比较。
#include <iostream>
#include <string>
using namespace std;
const int MaxN=200005;
const int MaxX=1e9+5;
int n,MinTime;
string dir;
int x[MaxN],d[MaxN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>dir;
for (int i=1; i<=n; i++) {
if (dir[i-1]=='L') d[i]=0;
else d[i]=1;
cin>>x[i];
}
MinTime=MaxX;
for (int i=2; i<=n; i++) {
if (dir[i-1]=='L' && dir[i-2]=='R') {
if (((x[i]-x[i-1])/2)<MinTime) MinTime=(x[i]-x[i-1])/2;
}
}
if (MinTime<MaxX) cout<<MinTime<<"\n";
else cout<<"-1\n";
}
A. Launch of Collider
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles
will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is
the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even
integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle
will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave
it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) —
the number of particles.
The second line contains n symbols "L"
and "R". If the i-th symbol equals "L",
then the i-th particle will move to the left, otherwise the i-th
symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn(0 ≤ xi ≤ 109) —
the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
input
4 RLRL 2 4 6 10
output
1
input
3 LLR 40 50 60
output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will
simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
题意:直线上有一些点,给定点的初始位置及运动方向(坐标都为偶数),点都以固定速度1运动,求最早哪一时刻会有两个点相遇。
思路:只要考虑相邻的点即可,如果相邻的两个点左边在向右运动,右半在向左运动,那么算出相遇时间,与最小值进行比较。
#include <iostream>
#include <string>
using namespace std;
const int MaxN=200005;
const int MaxX=1e9+5;
int n,MinTime;
string dir;
int x[MaxN],d[MaxN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>dir;
for (int i=1; i<=n; i++) {
if (dir[i-1]=='L') d[i]=0;
else d[i]=1;
cin>>x[i];
}
MinTime=MaxX;
for (int i=2; i<=n; i++) {
if (dir[i-1]=='L' && dir[i-2]=='R') {
if (((x[i]-x[i-1])/2)<MinTime) MinTime=(x[i]-x[i-1])/2;
}
}
if (MinTime<MaxX) cout<<MinTime<<"\n";
else cout<<"-1\n";
}
相关文章推荐
- java夯实基础系列:泛型
- 【杭电oj】1254 - 推箱子(dfs+bfs+优先队列)
- C语言-结构-笔记
- ListView
- [转载] 问题解决:FFmpeg视频编解码库,无法解析的外部信号
- 通过sql获得mysql的表结构
- 软件架构和设计
- div+css垂直居中显示
- 修改开源库Wheel后,能控制选中颜色及大小
- NYOJ-Color the fence
- 2016第一场多校赛
- 如何识别高级的验证码
- 73条日常Linux shell命令汇总
- Java如何读取Excel行列(getLastRowNum、getLastRowNum)
- 详解mysql建立索引的使用办法及优缺点分析
- 删除GitHub上repository里的某个文件
- [bzoj1562][NOI2009]变换序列(贪心+dfs/二分图最大匹配)
- codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)
- sort与asort与ksort区别
- 响应式 和 移动 web