Codeforces 652F Ants on a Circle

本文介绍了一个名为AntsonaCircle的精妙算法,该算法通过保持蚂蚁间相对顺序不变的原则,巧妙地解决了复杂碰撞场景下蚂蚁位置预测的问题。文章详细阐述了算法的核心思想,即只需找到离0最近的蚂蚁的最终位置,即可推算出所有蚂蚁的最终位置,通过调整t值实现位置更新,最后通过排序和计数确保了每个蚂蚁的正确归位。

Ants on a Circle

感觉这个思路好巧妙啊。

我们能发现不管怎么碰撞,初始态和最终态蚂蚁间的相对顺序都是一样的, 并且所占的格子也是一样的, 那么我们就只需要

找到其中一个蚂蚁的最终位置就能确定所有蚂蚁的位置了, 我们考虑找初始为止离0最近的那个蚂蚁的最终位置,我们能发现

蚂蚁从m-1->0  rk++, 蚂蚁从0->m-1 rk--, 在取模意义下rk就是那个蚂蚁的最终位置。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

LL n, m, t, cnt, X[N], ans[N];
char T[10];

struct node {
    LL s, d, id;
    bool operator < (const node& rhs) const {
        return s < rhs.s;
    }
} a[N];

int main() {
    scanf("%lld%lld%lld", &n, &m, &t);
    for(int i = 0; i < n; i++) {
        scanf("%lld%s", &a[i].s, T);
        a[i].s--; a[i].id = i;
        if(T[0] == 'L') a[i].d = -1;
        else a[i].d = 1;
    }
    sort(a, a + n);
    for(int i = 0; i < n; i++) {
        if(a[i].d == 1) {
            X[i] = (a[i].s + t) % m;
            cnt = (cnt + (a[i].s + t) / m) % n;
        } else {
            X[i] = (a[i].s - t) % m;
            cnt = (cnt + (a[i].s - t) / m) % n;
            if(X[i] < 0) X[i] += m, cnt = (cnt - 1) % n;
        }
    }
    sort(X, X + n);
    cnt = (cnt % n + n) % n;
    int now = 0;
    for(int i = cnt; i < n; i++) ans[a[now++].id] = X[i] + 1;
    for(int i = 0; i < cnt; i++) ans[a[now++].id] = X[i] + 1;
    for(int i = 0; i < n; i++) printf("%lld ", ans[i]);
    return 0;
}

/*
*/

 

转载于:https://www.cnblogs.com/CJLHY/p/10422818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值