uva 10881 - Piotr's Ants

点击打开链接uva 10881

思路:模拟
分析:
1 如果把蚂蚁看成是没有区别的点,那么只需计算出每只蚂蚁在t秒之后的位置即可。比如有三只蚂蚁,蚂蚁1 = (1,L),蚂蚁2 = (3 , L) , 蚂蚁3 = (4,L),则两秒钟之后,3只蚂蚁的位置分别为(3 , R) , (1 , L) , (2 , L)。
2 虽然从整体上讲,“掉头”等价于“对传而过”,但对于每只蚂蚁而言并不是这样。蚂蚁1的初始状态为(1 ,R),因此一定有一只蚂蚁在两秒种之后处于(3 , R)的状态,但这样的的蚂蚁不一定是蚂蚁1.换句话说,我们需要搞清楚目标状态中“谁是谁”。
3 所有蚂蚁的相对的位置是保持不变的,因此把所有目标位置从小到大排序,则从左到右的每个位置对应与初始状态下从左到右的每只蚂蚁。由于原题中蚂蚁不一定是按照从左到右的顺序输入,因此我们预处理计算出输入中第i只蚂蚁的序号order[]

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
struct Ant{
    int id;
    int p;
    int d;
    bool operator<(const Ant& a)const{
        return p < a.p;
    }
};
Ant before[MAXN] , after[MAXN];
char dir[3][10] = {"L","Turning","R"};
int order[MAXN];

int main(){
    int T, Case = 1;
    int l , t , n;
    scanf("%d" , &T);
    while(T--){
        scanf("%d%d%d" , &l , &t , &n);
        for(int i = 0 ; i < n ; i++){
           int p , d;
           char c;
           scanf("%d %c" , &p , &c);
           d = c == 'L' ? -1 : 1;
           before[i] = (Ant){i , p , d};//初始化
           after[i] = (Ant){0 , p+t*d , d};//这里的id是未知的
        }
        sort(before , before+n);
        for(int i = 0 ; i < n ; i++)
            order[before[i].id] = i;//输入的第i只蚂蚁是after中左数第after[order[i]]只蚂蚁
        sort(after , after+n);
        for(int i = 0 ; i < n-1 ; i++){
            if(after[i].p == after[i+1].p)
               after[i].d = after[i+1].d = 0;//说明正在碰撞
        }
        //输出结果
        printf("Case #%d:\n" , Case++);
        for(int i = 0 ; i < n ; i++){
            int a = order[i];//从第一个蚂蚁到最后一个蚂蚁
            if(after[a].p < 0 || after[a].p > l)//出界
               printf("Fell off\n");
            else
               printf("%d %s\n" , after[a].p , dir[after[a].d+1]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-09-20 05:57:50

uva 10881 - Piotr's Ants的相关文章

UVa 10714:Ants

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1655 原题: An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant rea

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic