Atcoder Grand Contest 013C - Ants on a Circle

Problem Statement

There is a circle with a circumference of L. Each point on the circumference has a coordinate value, which represents the arc length from a certain reference point clockwise to the point. On this circumference, there are N ants. These ants are numbered 1 through N in order of increasing coordinate, and ant i is at coordinate Xi.

The N ants have just started walking. For each ant i, you are given the initial direction Wi. Ant i is initially walking clockwise if Wi is 1; counterclockwise if Wi is 2. Every ant walks at a constant speed of 1 per second. Sometimes, two ants bump into each other. Each of these two ants will then turn around and start walking in the opposite direction.

For each ant, find its position after T seconds.

Constraints

  • All input values are integers.
  • 1N105
  • 1L109
  • 1T109
  • 0X1<X2<<XNL1
  • 1Wi2

Input

The input is given from Standard Input in the following format:

N L T
X1 W1
X2 W2
:
XN WN

Output

Print N lines. The i-th line should contain the coordinate of ant i after T seconds. Here, each coordinate must be between 0 and L1, inclusive.


Sample Input 1

Copy
3 8 3
0 1
3 2
6 1

Sample Output 1

Copy
1
3
0

1.5 seconds after the ants start walking, ant 1 and 2 bump into each other at coordinate 1.51 second after that, ant 1 and 3 bump into each other at coordinate 0.50.5 seconds after that, that is, 3 seconds after the ants start walking, ants 12 and 3 are at coordinates 13 and 0, respectively.


Sample Input 2

Copy
4 20 9
7 2
9 1
12 1
18 1

Sample Output 2

Copy
7
18
18
1

题意:在一个环上有N只蚂蚁,它们顺时针或者逆时针走,两只蚂蚁相遇就掉头,问最后每只蚂蚁的相对位置
题解:显然蚂蚁的相对位置不变,然后相遇掉头可以理解为改变编号
也就是说我们最后得到了一个排好序的x数组表示答案,我们只需要第一只蚂蚁的rank即可求出答案
考虑什么时候rank会变化
如果有一只蚂蚁从L-1走到0,那么1号蚂蚁的rank++
如果有一只蚂蚁从0走到L-1,那么1号蚂蚁的rank--
#include 
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;

typedef long long LL;
typedef pair < int, int > pa;

const int MAXN = 100010;

int n, x[MAXN], a[MAXN], w[MAXN], L, T;
LL cur;

int main()
{
#ifdef wxh010910
	freopen( "data.in", "r", stdin );
#endif
	scanf( "%d%d%d", &n, &L, &T );
	for( int i = 0 ; i < n ; i++ )
	{
		scanf( "%d%d", &a[ i ], &w[ i ] );
		if( w[ i ] == 1 )
		{
			x[ i ] = ( a[ i ] + T ) % L;
			cur += ( a[ i ] + T ) / L;
		}
		else
		{
			x[ i ] = ( a[ i ] - T ) % L;
			cur += ( a[ i ] - T ) / L;
			if( x[ i ] < 0 ) x[ i ] += L, cur--;
		}
	}
	cur = ( cur % n + n ) % n;
	sort( x, x + n );
	for( int i = cur ; i < n ; i++ ) printf( "%d\n", x[ i ] );
	for( int i = 0 ; i < cur ; i++ ) printf( "%d\n", x[ i ] );
}


你可能感兴趣的:(Atcoder Grand Contest 013C - Ants on a Circle)