UVA 10881 - Piotr's Ants(思维转换)

来源:互联网 发布:ipad办公 知乎 编辑:程序博客网 时间:2024/06/05 19:11

该题是一个考察想象力的经典例子,很考验人的观察和分析能力,要进行合理的等价转换。

首先,我们可以发现,相遇之后掉头,相当于直接穿过。   但是这样的话每只蚂蚁的位置和方向就乱了,好像很难处理 。  但是仔细观察可以发现,由于蚂蚁相遇即掉头,所以从左到右,每只蚂蚁的相对位置是固定的,也就是说,从左到右,一开始是第几只,最终还是第几只。   这样就可以解决输出顺序的问题了!  等等,还有方向呢?  这也很简单,我们说了, 相遇之后,相当于直接穿过,而相对位置又没有变化,所以其最终方向就是一直走到最后的方向,这一点不好说,建议仔细想想。

细节参见代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll INF = 1000000000000000000;const int maxn = 500000 + 5;ll T,n,L,t,m,kase = 0,ans[maxn],id[maxn];struct node{    int x, id, dir;    bool operator < (const node& rhs) const {        return x < rhs.x;    }}a[maxn],last[maxn];char dir[][10] = { "Turning", "L", "R" };char d[10];int main() {    scanf("%d",&T);    while(T--) {        scanf("%d%d%d",&L,&t,&n);        for(int i = 1; i <= n; i++) {            scanf("%d%s",&a[i].x,d);            a[i].dir = (d[0] == 'L' ? 1 : 2);            a[i].id = i;            if(a[i].dir == 2) last[i].x = a[i].x + t, last[i].dir = a[i].dir;            else last[i].x = a[i].x - t, last[i].dir = a[i].dir; last[i].id = 0;        }        sort(last+1,last+n+1);        sort(a+1,a+n+1);        for(int i = 1; i <= n; i++) {            ans[a[i].id] = i;            if(i == n) break;            if(last[i].x == last[i+1].x) last[i].dir = last[i+1].dir = 0;        }        printf("Case #%d:\n",++kase);        for(int i = 1; i <= n; i++) {            int id = ans[i];            if(last[id].x < 0 || last[id].x > L) printf("Fell off\n");            else printf("%d %s\n",last[id].x,dir[last[id].dir]);        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击