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+ ...
随机推荐
- 本周实验PSP0 过程文档
2016-03-12 项目总结: 日期\学习时间 听课 编写程序 阅读相关书籍 日总计 周一 110 0 30 140 周二 0 30 30 60 周三 0 40 0 40 周四 110 20 30 ...
- MacOS下安装Requests库及使用
大概框架 Request库的安装 爬取网页最好用的第三方库 直接安装即可(用于OS X) pip3 install requests request库的常用方法: request库一共有七个常用方法. ...
- HDU 5234 Happy birthday 01背包
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- CSS中px和em属性的特点与区别
详解px和em的特点和区别象素px是我们在定义CSS中经常用到的尺寸大小单位,而em在国外网站中经常被使用,px和em之间究竟有什么区别和特点呢?◆px像素(Pixel),相对长度单位.像素px是相对 ...
- L1正则化与L2正则化的理解
1. 为什么要使用正则化 我们先回顾一下房价预测的例子.以下是使用多项式回归来拟合房价预测的数据: 可以看出,左图拟合较为合适,而右图过拟合.如果想要解决右图中的过拟合问题,需要能够使得 $ ...
- css3 关于文字,字体属性(转载)
1.text-overflow属性(实现省略号效果) text-overflow用来设置是否使用一个省略标记(…)标示对象内文本的溢出. [语法] ❤text-overflow只是用来说明文字溢出时用 ...
- PAT 甲级 1035 Password
https://pintia.cn/problem-sets/994805342720868352/problems/994805454989803520 To prepare for PAT, th ...
- 在线webservice
腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx Disco: http:// ...
- linux的一些机制Signal, Fork,
signal(SIGCHLD, SignalHandler); 注册软中断,对应的api close(socket); ret=fork(): 父进程,返回子进程的pid. 子进程返回0, 出错返回& ...
- Robotium之“去哪儿旅行”
Robotium基于APK自动化测试,只有APK文件,没有源代码. Eclipse 默认的debug keystore可以在Windows->Preferences->Android-&g ...