题解:AT_agc013_c [AGC013C] Ants on a Circle

· · 题解

分享一种鏖战 3h 造出来的神奇新做法。

注意到蚂蚁最后的位置是确定下来的,就是上帝视角两只蚂蚁分头行动看成穿过对方。所以只需要知道两只蚂蚁的最终位置就确定了所有蚂蚁的位置(事实上两只蚂蚁无法穿过对方,所以在这两只蚂蚁中间的蚂蚁位置是固定的)。

你又注意到,链也是容易的,因为第一只蚂蚁的位置就是位置最小的蚂蚁的位置。但是环就没有这个优美的性质。

所以,我们强制破环为链,然后前面复制 t 份,后面复制 t 份,强制当成一个链来做!!

这样确实是正确的。时间复杂度 \Theta(n \log n)。

一份暴力 \Theta(nt) 代码:

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
/*
by qqqaaazzz
*/
int n,m,t;

struct Node{
    int st,op;
    int id;
};
Node a[500010];
Node all[500010];
Node ano[500010];
int cnt = 0;
Node xu[500010];
Node only[500010];
int ans[500010];

bool cmp(Node x,Node y){
    if(x.st==y.st) return x.id<y.id;
    return x.st<y.st;
}

int play(int x){
    return (x%m+m)%m;
}

signed main()
{
    cin >> n >> m >> t;
    for (int i=1;i<=n;i++){
        cin >> a[i].st;
        int T;
        cin >> T;
        if(T==2) a[i].op = -1;
        else a[i].op = 1;
        a[i].id = i;
    }
    for (int i=1;i<=n;i++){
        all[++cnt] = {a[i].st,a[i].op,a[i].id};
    }
    sort(all+1,all+cnt+1,cmp);

    int h = 0;

    for (int u=-t;u<=t;u++){
        for (int i=1;i<=n;i++){
            xu[++h] = {a[i].st+a[i].op*t+u*m,0,a[i].id};
        }
    }
    for (int i=1;i<=n;i++){
        only[i] = {play(a[i].st+a[i].op*t),0,a[i].id};
    }
    sort(only+1,only+n+1,cmp);
    sort(xu+1,xu+h+1,cmp);

    int MY = play(xu[t*n+1].st);
    int OH = play(xu[t*n+2].st);

    int qaq = 0;
    only[0] = only[n];

    for (int i=1;i<=n;i++) if(only[i].st==OH&&only[i-1].st==MY) qaq = (i-1==0? n:i-1);

    for (int i=1;i<=cnt;i++){
        ans[all[i].id] = only[qaq].st;
        qaq = qaq%n+1;
    }

    for (int i=1;i<=n;i++) cout << ans[i] << "\n";
    return 0;
}

下面是加上二分的正确代码:

#include <bits/stdc++.h>
#define int long long
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
/*
by qqqaaazzz
*/
int n,m,t,H;

struct Node{
    int st,op;
    int id;
};
Node a[500010];
Node all[500010];
Node ano[500010];
int cnt = 0;
Node only[500010];
int ans[500010];

bool cmp(Node x,Node y){
    if(x.st==y.st) return x.id<y.id;
    return x.st<y.st;
}

int play(int x){
    return (x%m+m)%m;
}

int check(int qwq){
    int cnt = 0;
    for (int i=1;i<=n;i++){
        int G = (qwq-a[i].st-a[i].op*t);
        if(G>=0) G = G/m;
        else G = (-G+m-1)/m*-1;
        G = min(G,H);
        if(G>=-H) cnt += (G-(-H)+1); 
    }
    return cnt;
}

int get(int P){
    //获得 xu[P]
    int l = -3e18,r = 3e18,res = 0;
    while(l<=r){
        int mid = (l+r)/2;
        if(check(mid)<P){
            l = mid+1;
        }
        else{
            res = mid;
            r = mid-1;
        }
    }
    return res;
}

signed main()
{
    cin >> n >> m >> t;
    for (int i=1;i<=n;i++){
        cin >> a[i].st;
        int T;
        cin >> T;
        if(T==2) a[i].op = -1;
        else a[i].op = 1;
        a[i].id = i;
    }
    for (int i=1;i<=n;i++){
        all[++cnt] = {a[i].st,a[i].op,a[i].id};
    }
    sort(all+1,all+cnt+1,cmp);

    int h = 0;
    H = t;

    for (int i=1;i<=n;i++){
        only[i] = {play(a[i].st+a[i].op*t),0,a[i].id};
    }
    sort(only+1,only+n+1,cmp);

    int MY = play(get(H*n+1));
    int OH = play(get(H*n+2));

    int qaq = 0;
    only[0] = only[n];

    for (int i=1;i<=n;i++) if(only[i].st==OH&&only[i-1].st==MY) qaq = (i-1==0? n:i-1);

    for (int i=1;i<=cnt;i++){
        ans[all[i].id] = only[qaq].st;
        qaq = qaq%n+1;
    }

    for (int i=1;i<=n;i++) cout << ans[i] << "\n";
    return 0;
}