NC222154. ChinoWithBall
描述
输入描述
There are two integers in the first input line
In next input lines, each line of two integers indicating the initial position and speed of the ball.
It is guaranteed in all input data that all the balls are in different positions at the beginning
输出描述
There are integers in a line, and the integer represents the position of the ball after seconds.
示例1
输入:
4 10 -10 1 10 -1 -7 0 5 0
输出:
-7 5 0 0
说明:
In the first case, assuming the positive direction is the right side, the ball and ball collide in the second, and the ball stays in place after the collision. The number ball continues to move to the right. In the second, the ball and the ball collided. After the collision, the ball stayed in place and the ball continued to move to the left. In the second, the ball and ball collided. Since the volume of the ball is negligible, it can be considered that the positions of both balls are at at the moment of collision.示例2
输入:
2 1 5 1 6 -1
输出:
5 6
说明:
In the second case,The two balls collided after seconds and then continued to move for seconds.C++ 解法, 执行用时: 52ms, 内存消耗: 3228K, 提交时间: 2021-05-22 13:50:23
#include <cstdio> #include <algorithm> #include <utility> using namespace std; #define mp make_pair #define fi first #define sc second #define PR pair<int,int> const int N=100010; int n,k,p[N],v[N],pos[N]; PR pr[N]; int main() { int i; scanf("%d%d",&n,&k); for(i=1;i<=n;i++) { scanf("%d%d",p+i,v+i); pr[i]=mp(p[i],i); } sort(pr+1,pr+n+1); for(i=1;i<=n;i++) pos[pr[i].sc]=i; for(i=1;i<=n;i++) p[i]+=v[i]*k; sort(p+1,p+n+1); for(i=1;i<n;i++) { printf("%d ",p[pos[i]]); } printf("%d",p[pos[n]]); return 0; }