关闭

Launch of Collider

标签: 贪心
45人阅读 评论(0) 收藏 举报
分类:

链接:http://codeforces.com/problemset/problem/699/A

题目:

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.



题意:一条直线上有几个点,分别想做或者向右运动,速度均为1m/s,求最早碰撞的时间。

分析:贪心,只有左边的球向右走,右边的球向左走才会碰撞,所以只要找到最小的(RL)间距就是答案。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int n;
char s[200010];
int num[200010];

int main()
{
	//freopen("in.txt", "r", stdin);
	while (cin >> n)
	{
		cin >> s;
		int ans = INT_MAX;
		cin >> num[0];
		for (int i = 1; i < n; i++)
		{
			cin >> num[i];
			if (s[i - 1] == 'R'&&s[i] == 'L')
			{
				ans = min(ans, num[i] - num[i - 1]);
			}
		}
		if (ans < INT_MAX)
			cout << ans/2 << endl;
		else
			cout << "-1" << endl;
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3188次
    • 积分:597
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    友情链接