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. mnist测试

    第一步:进入caffe目录 第二步:获取mnist数据集 ./data/mnist/get_mnist.sh 第三步:创建lmdb ./examples/mnist/create_mnist.sh 第 ...

  2. MySQL 日志功能详解

    MySQL日志分类 1:查询日志 :query log     2:慢查询日志:slow_query_log 查询执行时长超过指定时长的查询操作所记录日志     3:错误日志:error log   ...

  3. 数据输出保存生成word文档

    ob_start(); //打开缓冲区 $header_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office&qu ...

  4. 在Delphi中动态地使用SQL查询语句 Adoquery sql 参数 冒号

    在Delphi中动态地使用SQL查询语句 在一般的数据库管理系统中,通常都需要应用SQL查询语句来提高程序的动态特性.下面介绍如何在Delphi中实现这种功能.在Delphi中,使用SQL查询语句的途 ...

  5. 【bzoj2906】颜色 分块

    题目描述 给定一个长度为N的颜色序列C,对于该序列中的任意一个元素Ci,都有1<=Ci<=M.对于一种颜色ColorK来说,区间[L,R]内的权值定义为这种颜色在该区间中出现的次数的平方, ...

  6. P4622 [COCI2012-2013#6] JEDAN

    题目背景 COCI 题目描述 有N个数排成一行(数值代表高度),最初所有的数都为零,你可以选择连续的一段等高的数,将它们都增加1(除了开头和结尾那个数)如下图表示了两次操作: 现在有一些数字看不清了, ...

  7. [SDOI2011]黑白棋 kth - nim游戏

    题面 题面 题解 观察题目,我们可以发现,这个游戏其实就是不断再把对方挤到一边去,也就是黑子不断往左走,白子不断往右走. 因此可以发现,如果将黑白子按顺序两两配对,那么它们中间的距离会不断缩小,且每次 ...

  8. CF623D birthday 贪心 概率期望

    题意:n个人,玩抓人游戏,每抓住一个人都要猜这个人是谁.对于每一局,第i个人有$p_{i}$的概率被抓到.游戏结束当且仅当每个人都在某局中被抓到并且猜中自己的名字,求一个合适的策略来使得期望游戏局数最 ...

  9. 我是一个CPU:这个世界慢!死!了!

    最近小编看到一篇十分有意思的文章,多方位.无死角的讲解了CPU关于处理速度的理解,看完之后真是豁然开朗.IOT时代,随着科技的发展CPU芯片的处理能力越来越强,强大的程度已经超乎了我们的想象.今天就把 ...

  10. 9个基于Java的搜索引擎

    1.Java 全文搜索引擎框架 Lucene 毫无疑问,Lucene是目前最受欢迎的Java全文搜索框架,准确地说,它是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎.Luc ...