• 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();
    	}
    }
    
  • 相关阅读:
    CodeForces 620D Professor GukiZ and Two Arrays 双指针
    模板汇总 —— 最大团
    CodeForces 1105E Helping Hiasat 最大独立集
    CodeForces 925 C Big Secret
    CodeForces 979 D Kuro and GCD and XOR and SUM
    CodeForces 665E Beautiful Subarrays 字典树
    CodeForces 723F st-Spanning Tree
    CodeForces 103D Time to Raid Cowavans 询问分块
    博客园添加访问次数统计
    oracle转mysql总结
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/6729618.html
Copyright © 2020-2023  润新知