列表

详情


NC222154. ChinoWithBall

描述

There are small balls with same weight in horizontal smooth plane. And the volume of these balls can be negligible. All the balls are on the same straight line. There are three possibilities for their initial speed v_0 which is m/s, m/s and m/s.

The initial speed of means that the ball moves to the left along a straight line, the initial speed of means that the ball is still at first, and the initial speed of means that the initial speed of the ball moves to the right in a straight line.

A fully elastic collision occurs when two balls collide against each other.

Fully elastic collision formula is

Among them, m_1, m_2 represent the weight of the two objects that collided. v_1, v_2 represent the speed before the collision.

Don't be worried, this question is not a physics question. You only need to know the exchanging speed at which two objects of exactly the same weight when they collide.

Given the positions and speeds of balls at the beginning, find their positions seconds later.

输入描述

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 {1} and ball {3} collide in the {3rd} second, and the ball {1} stays in place after the collision. The number {3} ball continues to move to the right. In the {5th} second, the {No. 2} ball and the {No. 4} ball collided. After the collision, the {No. 2} ball stayed in place and the {No. 4} ball continued to move to the left. In the {10th} second, the ball {3} and ball {4} collided. Since the volume of the ball is negligible, it can be considered that the positions of both balls are at {0} at the moment of collision.

示例2

输入:

2 1
5 1
6 -1

输出:

5 6

说明:

In the second case,The two balls collided after {0.5} seconds and then continued to move for {0.5} 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;
}

上一题