E. Collisions
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.

The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be:

.

Your task is to find out, where each ball will be t seconds after.

Input

The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xivimi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0.

It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).

Output

Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.

Sample test(s)
input
2 9
3 4 5
0 7 8
output
68.538461538
44.538461538
input
3 10
1 2 3
4 -5 6
7 -8 9
output
-93.666666667
-74.666666667
-15.666666667

因为数据量很少,直接暴力求解即可,细节问题不少,WA了多次。更悲催的是,codeforces上用gun C++编译在test 24出错,但是本机测试答案却无误,用ms 2010编译就AC了。估计是浮点数的精度问题,两种编译器的处理方式有异……

AC Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string> using namespace std; const double MAXN = 10000000.000000;
const double eps = 10e-;
const double zero = 0.000000;
vector<int> idx; int main()
{
int n;
double t;
double x[], v[], m[];
while(scanf("%d %lf", &n, &t) != EOF)
{
for(int i = ; i < n; i++)
scanf("%lf %lf %lf", &x[i], &v[i], &m[i]);
while(fabs(t - zero) > eps)
{
double min = t;
double tmp;
idx.clear();
for(int i = ; i < n; i++)
{
for(int j = i + ; j < n; j++)
{
tmp = MAXN;
if(fabs(x[i] - x[j]) < eps) continue; //两个球碰撞之后的瞬间在同一位置
if(v[i] * v[j] > zero)
{
if(v[j] == v[i]){}
else if(x[i] > x[j])
tmp = (x[i] - x[j]) / (v[j] - v[i]);
else
tmp = (x[j] - x[i]) / (v[i] - v[j]);
}
else
{
if(v[j] == zero && v[i] == zero){}
else if(x[i] > x[j] && v[i] <= zero && v[j] >= zero)
tmp = (x[i] - x[j]) / (-v[i] + v[j]);
else if(x[i] < x[j] && v[i] >= zero && v[j] <= zero)
tmp = (x[j] - x[i]) / (-v[j] + v[i]);
}
if(tmp > zero && min >= tmp) //可能有多对球在不同地点同时碰撞,故而min>=tmp而非min>tmp
{
if(min > tmp)
{
idx.clear();
//当多对球同时碰撞时才需要存储多对下标,不然一定要清空原来
//存储的一对下标
}
min = tmp;
idx.push_back(i);
idx.push_back(j);
}
}
}
t -= min;
for(int i = ; i < n; i++)
{
x[i] = x[i] + v[i] * min;
}
int i, j;
for(vector<int>::iterator it = idx.begin(); it != idx.end(); it += )
{
i = *it, j = *(it + );
double vi = v[i];
//更新v[j]时需要用到v[i],而v[i]在更新v[j]前已经更新,故而要备份v[i]
v[i] = ((m[i] - m[j])*v[i] + 2.000000 * m[j]*v[j]) / (m[i] + m[j]);
v[j] = ((m[j] - m[i])*v[j] + 2.000000 * m[i]*vi) / (m[j] + m[i]);
}
}
for(int i = ; i < n; i++)
printf("%.5lf\n", x[i]);
}
}

Codeforces Beta Round #34 (Div. 2) E. Collisions的更多相关文章

  1. Codeforces Beta Round #34 (Div. 2)

    Codeforces Beta Round #34 (Div. 2) http://codeforces.com/contest/34 A #include<bits/stdc++.h> ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. Java 成员初始化顺序

    package com.cwcec.test; class Fu { int num = 5; //构造代码块 { System.out.println("Fu constructor co ...

  2. 奇异值分解(SVD) --- 几何意义 (转载)

    PS:一直以来对SVD分解似懂非懂,此文为译文,原文以细致的分析+大量的可视化图形演示了SVD的几何意义.能在有限的篇幅把 这个问题讲解的如此清晰,实属不易.原文举了一个简单的图像处理问题,简单形象, ...

  3. php多维数组排序 3

    本文实例讲述了php简单实现多维数组排序的方法.分享给大家供大家参考,具体如下: 之前在做一个功能的时候,必须要把数据放到二维数组里并且排序,然后上网找找解决思路, 这时候会用到array_multi ...

  4. linux php 访问sql server设置

    1.安装freeTDS wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz 1.1.进入到你下载的目录然后解压.tar - ...

  5. TP中循环遍历

    循环遍历(重点) 在ThinkPHP中系统提供了2个标签来实现数组在模版中的遍历: volist标签.foreach标签. Volist语法格式: Foreach语法格式: 从上述的语法格式发现vol ...

  6. lxs1314 is not in the sudoers file. This incident will be reported.

    虚拟机下面  普通用户用sudo执行命令时报"xxx is not in the sudoers file.This incident will be reported"错误,解决 ...

  7. python常用模块collections os random sys

    Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...

  8. [洛谷P1642]规划

    题目大意:有一棵$n(n\leqslant100)$个点的树,每个点有两个权值$a,b$,要求选择一个$m$个点的连通块$S$,最大化$\dfrac{\sum\limits_{i\in S}a_i}{ ...

  9. [TJOI2013]单词 AC自动机

    题面: 洛谷 题解: 很久之前做的题了,只不过之前一直90....最近才发现是哪里写错了. 我们对字符集建AC自动机. 首先考虑一个暴力的做法,把文章当做一个长串,直接在自动机上跳,但是我们会发现,这 ...

  10. 如何用Qt Python创建简单的桌面条形码应用

    Qt for Python可以快速跨平台的GUI应用.这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用. 安装Qt for Python 官方站点 ...