CF

题目传送门

题解:

  先观察蚂蚁相撞, 可以发现, 如果我们将相撞的2个蚂蚁互换位置的话,蚂蚁相当于没有碰撞体积,直接穿过去了。所以我们可以直接计算出最终哪些位置上会有蚂蚁。

  接下来就需要知道蚂蚁们的最终是走到哪个位置上。 需要先明白的是, 蚂蚁的相对位置是不会发生变化的,他的左边和右边的蚂蚁是不会发生改变的。

  我们通过记录顺时针第一只蚂蚁是什么编号。

  假设X是第一只蚂蚁的编号。

  如果有一只蚂蚁从 m-1 的位置走到 0 的位置,那么X会变成 X - 1。

  如果有一只蚂蚁从 0 的位置走到 m-1的位置, 那么X会变成 X +1。

  接下来我们只需要算所有的蚂蚁左走了几圈,右走了几圈就可以算出X最终停在哪里了。

代码:

/*
code by: zstu wxk
time: 2019/02/23
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 3e5 + 100;
LL n, m, t;
struct Node{
    int p, id, dir;
    bool operator<(const Node & x) const{
        return p < x.p;
    }
}A[N];
int now[N];
int ans[N];
void Ac(){
    char op[10];
    for(int i = 0; i < n; ++i){
        scanf("%d%s", &A[i].p, op);
        --A[i].p;
        if(op[0] == 'L') A[i].dir = -1;
        else A[i].dir = 1;
        A[i].id = i;
    }
    sort(A, A+n);
    int p = 0;
    for(int i = 0; i < n; ++i){
        now[i] = (A[i].dir * t % m + m + A[i].p) % m;
        p = (p - (A[i].p + A[i].dir * t - now[i]) / m + n) % n;
        p = (p%n+n)%n;
    }
    sort(now, now+n);
    for(int i = 0; i < n; ++i)
        ans[A[(i+p)%n].id] = now[i]+1;
    for(int i = 0; i < n; ++i)
        printf("%d ", ans[i]);
}
int main(){
    while(~scanf("%I64d%I64d%I64d", &n, &m, &t)){
        Ac();
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/MingSD/p/10423027.html