atCoder Ants on a Circle(又是蚂蚁问题。。。)

atCoder Ants on a Circle(又是蚂蚁问题。。。)

传送门

题意:一个圈,蚂蚁在上面以相同的速度和不同的方向走,问t秒后它们各自的位置。

解法:和经典的蚂蚁问题一致,把相撞的情况看做是穿过,我们不需要关心穿过的蚂蚁去哪儿了,它们的位置是相对不变的。然而。。。这里的路线是一个圈,势必会出现原本排在第一的蚂蚁跑到了尾部,又或是排在尾部的蚂蚁跑到了第一,也就是说位置是会变得。
但是我们只需要将首部的蚂蚁移动<0看做是这只蚂蚁被顶了上去,某只蚂蚁移动>=l看做试将一系列蚂蚁顶了回去。就能锁定原本首部蚂蚁的位置,答案也就出来了

import java.io.*;
import java.util.*;

class MyInputStream extends InputStream {
	public BufferedInputStream bis = new BufferedInputStream(System.in);

	public int read() throws IOException {
		int i;
		while ((i = bis.read()) < 48)
			if (i == -1)
				return -1;
		int temp = 0;
		while (i > 47) {
			temp = temp * 10 + i - 48;
			i = bis.read();
		}
		return temp;
	}
}

public class Main {

	static final int N = 100005;
	static final int inf = 0x3f3f3f3f;
	static final double eps = 1e-6;
	static int a[] = new int[N];

	public static void main(String[] args) throws IOException {
		MyInputStream cin = new MyInputStream();
		int n = cin.read(), l = cin.read(), t = cin.read();
		int tmp, cnt = 0, d;
		for (int i = 0; i < n; i++) {
			a[i] = cin.read();
			d = cin.read();
			if (d == 1) {
				tmp = a[i] + t;
				a[i] = tmp % l;
				cnt += tmp / l;
			} else {
				tmp = a[i] - t;
				a[i] = tmp % l;
				cnt += tmp / l;
				if (a[i] < 0) {
					a[i] += l;
					cnt--;
				}
			}
		}
		Arrays.sort(a, 0, n);
		cnt %= n;
		if (cnt < 0)
			cnt += n;
		cnt %= n;
		for (int i = cnt; i < cnt + n; i++) {
			int j = i % n;
			System.out.println(a[j]);
		}
		cin.close();
	}
}
原文地址:https://www.cnblogs.com/zsyacm666666/p/6729618.html