Codeforces Beta Round #34 (Div. 2) E. Collisions
2 seconds
256 megabytes
standard input
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.
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: xi, vi, mi (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 n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
2 9
3 4 5
0 7 8
68.538461538
44.538461538
3 10
1 2 3
4 -5 6
7 -8 9
-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的更多相关文章
- Codeforces Beta Round #34 (Div. 2)
Codeforces Beta Round #34 (Div. 2) http://codeforces.com/contest/34 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- 漫漫征途,java开发(未完待续)
前言 2018年,大二上,有幸加入服务外包实验室的考核,在考核中,主动加入xxx项目的后端,一是为了积累项目经验,二是为了学到更多东西,进入了之后发现原来要学的这么多,时间这么紧!但唯有学习! 心得体 ...
- 模拟登入教务处(header)
import HTMLParser import urlparse import urllib import urllib2 import cookielib import string import ...
- 博弈---巴什博奕(Bash Game)(博弈入门)
巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走 ...
- lintcode-477-被围绕的区域
477-被围绕的区域 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 'X' 填充满. 样例 给出二维矩阵: X X X X X O O X X X O X X O ...
- lintcode-473-单词的添加与查找
473-单词的添加与查找 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则 ...
- [并查集] How Many Tables
题目描述 Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants ...
- Visual C++ 8.0对象布局
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...
- IP ,路由
ifconfig 命令 ip信息 enp0s3: flags=4163<UP(已经启用),BROADCAST(支持广播),RUNNING,MULTICAST(支持多播)> ...
- Mybatis 中 sql 语句的占位符 #{} 和 ${}
#{} 表示一个占位符号,通过 #{} 可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换.#{} 可以有效防止 sql注入. #{} ...
- IIS部署时failed to execute url 解决方法
web.config中增加如下节点: <system.webServer> <validation validateIntegratedModeConfiguration=&quo ...