【思维】AtCoder Grand Contest(013)C[Ants on a Circle]题解

来源:互联网 发布:3d studio max mac版 编辑:程序博客网 时间:2024/05/17 07:41

题目概述

n 只蚂蚁在长度为 L 的环上的不同位置(位置按编号递增),蚂蚁相撞会立刻转向,求最后每只蚂蚁的位置。

解题报告

栽了不知道多少次的题目……首先老套路,蚂蚁相撞看作穿过,最后相对位置不变。

但是环是什么鬼?想一下发现其实就是让我们确定某一只蚂蚁,从而确定所有的蚂蚁。

方便起见,我们用第一只蚂蚁来分析。刚开始,第一只蚂蚁位置最靠前,如果没有任何一只蚂蚁走过了临界点 0 ,则第一只蚂蚁将一直最靠前。但如果现在出现了一只蚂蚁顺时针走过了临界点,说明他前面的蚂蚁被“推”走了(因为相对位置不会改变),也就是说,第一只蚂蚁的位置会 +1 。同理,如果逆时针走过了临界点,第一只蚂蚁的位置会 1

示例程序

#include<cstdio>#include<cctype>#include<algorithm>using namespace std;const int maxn=100000;int n,L,T,a[maxn+5],ans[maxn+5];#define Eoln(x) ((x)==10||(x)==13||(x)==EOF)inline char readc(){    static char buf[100000],*l=buf,*r=buf;    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);    if (l==r) return EOF;return *l++;}inline int readi(int &x){    int tot=0,f=1;char ch=readc(),lst='+';    while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}    if (lst=='-') f=-f;    while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+ch-48,ch=readc();    return x=tot*f,Eoln(ch); }int main(){    freopen("ants.in","r",stdin);    freopen("ants.out","w",stdout);    readi(n);readi(L);readi(T);int fst=0;    for (int i=0,x,f;i<n;i++)    {        readi(x);readi(f);        if (f==1) ans[i]=x+T,fst+=ans[i]/L,ans[i]%=L; else        {ans[i]=x-T;fst+=ans[i]/L;ans[i]%=L;if (ans[i]<0) fst--,ans[i]+=L;}    }    sort(ans,ans+n);fst=(fst%n+n)%n;    for (int i=fst;i<n;i++) printf("%d\n",ans[i]);    for (int i=0;i<fst;i++) printf("%d\n",ans[i]);    return 0;}
原创粉丝点击