CCF-20180302 碰撞的小球
题意: 一条线段上有很多小球, 小球在运动, 他们之间可以碰撞。求某时间后他们的位置。
分析: 直接进行模拟, 小球在线段上的位置当出现碰撞或者边界直接转换运动方向。
代码: 100分。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 111;
const int MAXL = 1111;
int arr[MAXN];
int dis[MAXN];
int vis[MAXL];
int main ()
{
int n, t, l;
cin >> n >> l >> t;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
if (arr[i] == l)
dis[arr[i]] = -1;
else
dis[arr[i]] = 1;
}
for (int tm = 0; tm < t; tm++)
{
memset (vis, 0, sizeof (vis));
for (int i = 0; i < n; i++)
{
vis[dis[i] + arr[i]]++;
}
for (int i = 0; i < n; i++)
{
int p = dis[i] + arr[i];
if (vis[p] >= 2 || p >= l || p <= 0)
{
dis[i] *= -1;
}
arr[i] = p;
}
}
cout << arr[0];
for (int i = 1; i < n; i++)
cout << " " << arr[i];
cout << endl;
return 0;
}