试题编号: 201803-2
试题名称: 碰撞的小球
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处。有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒。
当小球到达线段的端点(左端点或右端点)的时候,会立即向相反的方向移动,速度大小仍然为原来大小。
当两个小球撞到一起的时候,两个小球会分别向与自己原来移动的方向相反的方向,以原来的速度大小继续移动。
现在,告诉你线段的长度L,小球数量n,以及n个小球的初始位置,请你计算t秒之后,各个小球的位置。
提示
因为所有小球的初始位置都为偶数,而且线段的长度为偶数,可以证明,不会有三个小球同时相撞,小球到达线段端点以及小球之间的碰撞时刻均为整数。
同时也可以证明两个小球发生碰撞的位置一定是整数(但不一定是偶数)。
输入格式
输入的第一行包含三个整数n, L, t,用空格分隔,分别表示小球的个数、线段长度和你需要计算t秒之后小球的位置。
第二行包含n个整数a1, a2, …, an,用空格分隔,表示初始时刻n个小球的位置。
输出格式
输出一行包含n个整数,用空格分隔,第i个整数代表初始时刻位于ai的小球,在t秒之后的位置。
样例输入
3 10 5
4 6 8
样例输出
7 9 9
样例说明
初始时,三个小球的位置分别为4, 6, 8。
一秒后,三个小球的位置分别为5, 7, 9。
两秒后,第三个小球碰到墙壁,速度反向,三个小球位置分别为6, 8, 10。
三秒后,第二个小球与第三个小球在位置9发生碰撞,速度反向(注意碰撞位置不一定为偶数),三个小球位置分别为7, 9, 9。
四秒后,第一个小球与第二个小球在位置8发生碰撞,速度反向,第三个小球碰到墙壁,速度反向,三个小球位置分别为8, 8, 10。
五秒后,三个小球的位置分别为7, 9, 9。
样例输入
10 22 30
14 12 16 6 10 2 8 20 18 4
样例输出
6 6 8 2 4 0 4 12 10 2
数据规模和约定
对于所有评测用例,1 ≤ n ≤ 100,1 ≤ t ≤ 100,2 ≤ L ≤ 1000,0 < ai < L。L为偶数。
保证所有小球的初始位置互不相同且均为偶数。
题目分析:
使用结构体保存当前小球的位置状态,然后根据时间的迁移,更改位置和状态,最后到了时间点输出。具体实现看代码
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
typedef struct{
int ballid; //小球序号
int direct; //运动方向
int pos; //位置
}Ball;
bool cmp(Ball b1 , Ball b2){ //小球位置排序
return b1.pos < b2.pos;
}
bool cmp2(Ball b1 , Ball b2){ //小球序号排序
return b1.ballid < b2.ballid;
}
int main(){
int n , L , end , pos; //end表示最后时间
Ball ball;
vector<Ball> vc;
while(cin >> n >> L >> end){
for(int i = 0 ; i < n ; i++){
cin >> ball.pos;
ball.direct = 1;
ball.ballid = i + 1;
vc.push_back(ball);
}
int time = 1;
while(time <= end){
for(int i = 0 ; i < vc.size(); i++){
vc[i].pos = vc[i].pos + vc[i].direct;
}
sort(vc.begin() , vc.end(), cmp); //按小球此时的位置排序,是为了下面的小球碰撞变换方向
for(int i = 0; i < vc.size(); i++){
if(vc[i].pos == L){ //到两个端点了变换方向
vc[i].direct = -1;
}else if(vc[i].pos == 0){
vc[i].direct = 1;
}
}
for(int i = 0; i < vc.size() - 1; i++){
if(vc[i].pos == vc[i + 1].pos){ //两个小球碰在一起了,变换方向
int temp = vc[i].direct;
vc[i].direct = vc[i + 1].direct;
vc[i + 1].direct = temp;
}
}
time++;
}
sort(vc.begin() , vc.end(), cmp2); //按小球的序号排序
for(int i = 0 ; i < vc.size(); i++){
cout << vc[i].pos << " ";
}
cout << endl;
vc.clear();
}
return 0;
}
/* 实际上这种方法和我的是一样的,他在过程中没有重新对小球位置进行排序,而是使用了双重
循环,查找碰撞的小球,所以最后也不需要按小球的下标进行排序
#include <iostream>
using namespace std;
const int L = 1000;
int pos[L + 1], step[L + 1];
int main()
{
int n, l, t;
cin >> n >> l >> t;
for(int i = 0; i < n; i++) {
cin >> pos[i];
// 开始往右走,到达两端则回头
step[i] = 1;
if(pos[i] == l || pos[i] == 0)
step[i] = -step[i];
}
for(int i = 0; i < t; i++) {
// 走一步
for(int j = 0; j < n; j++) {
pos[j] += step[j];
// 到达两端则回头
if(pos[j] == l || pos[j] == 0)
step[j] = -step[j];
}
// 判断是否碰头,碰头则掉头(要避免重复比较)
for(int j = 0; j < n; j++)
for(int k = j + 1; k < n; k++)
if(pos[k] == pos[j])
step[k] = -step[k], step[j] = -step[j];
}
for(int i = 0; i < n; i++)
cout << pos[i] << " ";
cout << endl;
return 0;
}
*/
/* 第二次,实际上都没有考虑一种情况,此时的小球已经在边上。但是通过了,就无关大雅
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
const int N = 100;
typedef struct{
int id;
int pos;
int dir;
}Ball;
Ball ball[N];
bool cmp1(Ball b1,Ball b2){
return b1.pos < b2.pos;
}
bool cmp2(Ball b1,Ball b2){
return b1.id < b2.id;
}
int main()
{
int n , l , end ;
while(cin >> n >> l >> end){
for(int i = 0; i < n ; i++){
cin >> ball[i].pos;
ball[i].id = i + 1;
ball[i].dir = 1;
}
int t = 1;
while(t <= end){
for(int i = 0 ; i < n ; i++){
ball[i].pos = ball[i].pos + ball[i].dir;
if(ball[i].pos == l || ball[i].pos == 0){
ball[i].dir = -ball[i].dir;
}
}
sort(ball , ball + n , cmp1);
for(int i = 0 ; i < n - 1; i++){
if(ball[i].pos == ball[i+1].pos){
ball[i].dir = -ball[i].dir;
ball[i+1].dir = -ball[i+1].dir;
}
}
t++;
}
sort(ball , ball + n , cmp2);
for(int i = 0 ; i < n ; i++){
cout << ball[i].pos << " ";
}
cout << endl;
}
return 0;
}
*/