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. 404_NOTE_Foung_软工6

    目录 NABCD分析引用 N(Need,需求): A(Approach,做法): B(Benefit,好处): C(Competitors,竞争): D(Delivery,交付): 初期 中期 个人贡 ...

  2. Java 数组转字符

    public static String toString(int[] arr){ String temp = ""; for(int i = 0;i<arr.length; ...

  3. MySQL 忘记root密码怎么办

    前言:记住如果忘记root密码,在启动MySQL的时候,跳过查询授权表就ok了. 对于RedHat 6 而言 (1)启动mysqld 进程时,为其使用:--skip-grant-tables --sk ...

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

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

  5. freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)

    exists:由空值测试运算符的引入,它被废弃了. exp1?exists 和 exp1??是一样的, ( exp1)?exists 和(exp1)??也是一样的. if_exists:由默认值运算符 ...

  6. 如何在Eclipse配置PyDev插件

    如何在Eclipse配置PyDev插件 | 浏览:1733 | 更新:2014-04-21 11:36 1 2 3 4 5 分步阅读 Eclipse配置PyDev插件 方法/步骤   从 Eclips ...

  7. (转)用MongoDB 实现优酷API 缓存

    由于众所周知的原因, 邪恶的企业优酷于九月的某一天开始禁止第三方播放器加载视频API, 我不得不设置一个反向代理来绕过Flash 的跨域限制. 自此服务器压力激增, 导致用户体验大为劣化. 为了减少服 ...

  8. HDU4788_Hard Disk Drive

    水题. 但是我写挫了一个地方,Wa了三发.好吧,不能忍了. 还有,本屌不知道如何用printf输出%,哪位学过C++的大仙知道这是什么情况?  告诉我一声啊. #include <iostrea ...

  9. iOS----MRC(手动内存管理)

    1.MRC是什么,有什么用? 在苹果开发中,我们是没有垃圾回收机制的.所以在ARC推出之前,我们苹果开发程序员需要通过手动代码的形式尽量严密的管理我们的App的内存: ---------------- ...

  10. [九]SpringBoot 之 定时任务

    代码: package me.shijunjie.config; import org.springframework.context.annotation.Configuration; import ...