zoukankan      html  css  js  c++  java
  • Codeforces Round #363 (Div. 2) A. Launch of Collider

    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米每微秒,运动方向是R或者L,求两个粒子第一次碰撞时的时间。

    分析:粒子什么时候会碰撞?当一个粒子运动方向R,而它旁边的粒子运动方向L,则这两个粒子最终会碰撞,需要的时间为坐标差的一半,找到找到最小值。初始化这个最小值为-1,即所有粒子都不会碰撞;

     1 #include <iostream>
     2 #include <set>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int main(){
     8     int n, i;
     9     scanf("%d", &n);//粒子数为n
    10     
    11     char *s = new char [n+1];
    12     int *a = new int [n];
    13     scanf("%s", s);
    14     
    15     for(i = 0; i < n; i++){
    16         scanf("%d", &a[i]);
    17     }
    18     
    19     int minn = -1;
    20     for(i = 0; i < n - 1; i++){
    21         if(s[i] == 'R' && s[i+1] == 'L'){
    22             if(minn == -1){
    23                 minn = a[i+1] - a[i];
    24             }
    25             else {
    26                 minn = min(minn, a[i+1] - a[i]);
    27             }
    28         }
    29     }
    30     
    31     if(minn != -1)
    32         printf("%d
    ", minn / 2);
    33     else
    34         printf("%d
    ", minn);
    35     delete [] s;
    36     delete [] a;
    37     return 0;
    38 }
  • 相关阅读:
    前端html--position,旋转变换
    前端html--背景,文本,过渡效果属性
    前端html--margin,border,padding,盒模型
    前端html--css样式选择器
    前端html--超链接,表格,表单属性
    jQuery 中的事件绑定
    JavaScript简单了解
    private,protected,public和default的区别
    html基础和CSS选择器
    JSP小结
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5720285.html
Copyright © 2011-2022 走看看