• POJ 2674 Linear world(弹性碰撞)


    Linear world
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 4426   Accepted: 1006

    Description

    The Disc, being flat, has no real horizon. Any adventurous sailors who get funny ideas from staring at eggs and oranges for too long and set out for the antipodes soon learned that the reason why distant ships sometimes looked as though they were disappearing over the edge of the world was that they were disappearing over the edge of the world. (Terry Pratchett -Colour of Magic) 
    Not so long time ago people used to believe that they live on 2-D world and if they will travel long enough in one direction, they will fall down over the edge. Even when it was proved that the Earth is rounded some of them were still afraid to travel to the southern hemisphere. 
    Try to imagine one 1-D (linear) world. On such world there are only two possible directions (left and right). All inhabitants of such world were created exactly at the same time and suddenly all of them start to move (all with same constant velocity) in one or the other direction. If two inhabitants encounter each other, they politely exchange greetings and then they turn around and start to move in an opposite direction. When an inhabitant reaches the end of the world he falls away and disappears. 
    Your task is to determine, for a given scenario of creation, which inhabitant and when (counting from the moment of creation) will be the last one to fall away. You can assume that the time required to exchange greetings and turn around is 0.

    Input

    The input consists of multiple descriptions (data sets) of the creation moment. File structure is as follows: 

    LV 
    DIR POS NAME 
    ... 
    The first line defines the number of inhabitants (N<32000). Data set starting with value N=0 represents the end of the input file. The second line contains length of the world L(float) and velocity of inhabitants V(float). Both values are always positive. In next N lines the data about inhabitants are given in an order of increasing POS (positive direction): 
    DIR – initial direction ('p' or 'P' for positive and 'n' or 'N' for negative) 
    POS – position in the time of creation (0<=POS<=L) 
    NAME – name of inhabitant (string up to 250 characters) 
    Input values within one line are separated with at least one space and there will be no empty lines in input. You may assume that input is always correct and that each data set has only one unique solution.

    Output

    The output consists of one line per each input data set. The first value should be the time when the last inhabitant will fall of the linear world counting from the moment of creation. Value should be printed truncated to two decimal places in a field 13 characters wide. The second value should be the name of the inhabitant. Values should be separated with single space character.

    Sample Input

    1   
    13.5 2   
    p 3.5 Smarty  
    4  
    10  1  
    p  1  Helga  
    n 3 Joanna  
    p  5  Venus  
    n  7  Clever  
    0 

    Sample Output

             5.00 Smarty
             9.00 Venus

    Source

     
    题意:在一个线性世界,有n个居民,每个人都在自己的初始位置pos上,运动方向分别为P(正方向)和N(负方向)。 线性世界长度为L,所有居民移动速度都为v。居民们相遇时,就会自动掉头。从初始时刻开始运动,问最后一个掉下去的运动了多长时间,且输出姓名。
     
    题解:运动速度一致,且掉头不花费时间。典型的弹性碰撞模型。我们可以忽略碰撞掉头的影响,将碰撞掉头看成两个居民交换姓名,擦肩而过。 所以最长时间即是离端点最远的居民的运动时间。  哪么谁是最后一个掉下的去的呢?这个要看运动时间最长的居民和哪些居民交换了姓名,最后一个交换的就是最后一个掉下去的。 怎么找到这个人呢?居民们速度一致,所有在前进路上只会与方向相反的居民碰撞。统计出前进路上方向不一致的人数就行了。
    1. 两人碰面后方向相反速度不变,可以认为两人只是交换了名字,  
    2. 仍旧按原来的方向行走。那么只需找出距离端点最远的人,再  
    3. 找出走到端点时此人的名字即可。怎么找出对应的名字呢,  
    4. 只需找出此人走到尽头前遇到的方向相反的人数即可。  
    5. 例如-<<- -<-  
    6.      1  2  3  4  5  
    7. 假设第一个人是最远的,因为2,3,5与之方向相反,因此  
    8. 他的名字将变化三次(5的方向和速度给4,5带着4的速度和方向  
    9. 走到尽头,最后1依次与2,3,4交换)。可以看出1,5之间有  
    10. 方向向右的并不影响(即使有多个也是一样的),1最终只交换  
    11. 三次速度,因为有三人跟他方向相反。而且1最后用的名字一定是  
    12. name[1+3],因为五个人的相对位置是不会变的,也就是说1只可能  
    13. 与2相交换,变成2,而不可能直接与3交换,以此类推,因此最后  
    14. 的名字一定是name[1+3].  
     1 #include<cstdio>  
     2 #include<algorithm>  
     3 #include<iostream>  
     4 #include<math.h>  
     5 using namespace std;  
     6 struct node  
     7 {  
     8     char d[2];  
     9     double dec;  
    10     char name[255];  
    11 } c[33000];  
    12 bool cmp(const node& a,const node& b)  
    13 {  
    14     return a.dec<b.dec;  
    15 }  
    16 int main()  
    17 {  
    18     int n,i,j;  
    19     double L,V;  
    20     while(~scanf("%d",&n))  
    21     {  
    22         if(!n)break;  
    23         scanf("%lf%lf",&L,&V);  
    24         for(i=1; i<=n; i++)  
    25         {  
    26             scanf("%s %lf %s",&c[i].d,&c[i].dec,&c[i].name);  
    27         }  
    28         sort(c+1,c+n+1,cmp);  
    29         int id;  
    30         double maL=0;  
    31         for(i=1; i<=n; i++)  
    32         {  
    33             double R;  
    34             if(c[i].d[0]=='p'||c[i].d[0]=='P')  
    35                 R=L-c[i].dec;  
    36             else R=c[i].dec;  
    37             if(R>maL)  
    38             {  
    39                 maL=R;  
    40                 id=i;  
    41             }  
    42         }  
    43         int ans=0;  
    44         if(c[id].d[0]=='p'||c[id].d[0]=='P')  
    45         {  
    46             for(j=id+1; j<=n; j++)  
    47                 if(c[j].d[0]=='n'||c[j].d[0]=='N')  
    48                     ans++;  
    49             ans=id+ans;  
    50         }  
    51         else  
    52         {  
    53             for(j=id-1; j>=1; j--)  
    54                 if(c[j].d[0]=='p'||c[j].d[0]=='P')  
    55                     ans++;  
    56             ans=id-ans;  
    57         }  
    58         maL=maL/V;  
    59         printf("%13.2lf %s
    ",floor(maL*100)/100.00,c[ans].name);  
    60     }  
    61     return 0;  
    62 }  
    View Code

    思路:

    很久之前做的一道思路题目了。 这个题比较坑把 记录一下。

    两只蚂蚁碰面后相当于不回头一直往前走。

    那么我们可以记录下来每只蚂蚁假如不碰面掉落的时间,取个最大值就是最后一只蚂蚁掉落的时间,在把那个时间对应的每只蚂蚁位置记录下来,还在线上的就是最后一只蚂蚁。

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 const int N = 32000;
     6 char name[N][251];
     7 
     8 int main()
     9 {
    10     int n;
    11     double l, v;
    12 
    13     while (cin >> n && n)
    14     {
    15         cin >> l >> v;
    16         int pnum = 0;
    17         double pmaxt = -1, nmaxt = -1;
    18         char dir[2];
    19         double pos;
    20         for (int i = 0; i < n; i++)
    21         {
    22             scanf("%s%lf%s", dir, &pos, name[i]);
    23             if (dir[0] == 'p' || dir[0] == 'P')
    24             {
    25                 pnum++;
    26                 if (pmaxt < 0)//第一个人(最大的pmax)
    27                     pmaxt = (l - pos) / v;
    28             }
    29             else
    30             {
    31                 nmaxt = pos / v;//最后一个人(最大的nmax)
    32             }
    33         }
    34         //结果小数点2位之后的数是直接截断的,并不是四舍五入,因此不可以直接打印,要先处理再打印
    35         if (pmaxt > nmaxt)
    36             printf("%13.2lf %s
    ", (int)(pmaxt * 100) / 100.0, name[n - pnum]);
    37         else
    38             printf("%13.2lf %s
    ", (int)(nmaxt * 100) / 100.0, name[n - pnum - 1]);
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    nginx+tomcat实现动静分离
    redis主从配置+哨兵模式
    字符串去重
    MySql数据库笔试题总结
    ElasticSearch入门 第一篇:Windows下安装ElasticSearch
    Java NIO 读取文件、写入文件、读取写入混合
    Java NIO 与 IO之间的区别
    第2章 Python基础-字符编码&数据类型 综合 练习题
    第2章 Python基础-字符编码&数据类型 字典 练习题
    第2章 Python基础-字符编码&数据类型 列表&元祖 练习题
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271176.html
Copyright © 2020-2023  润新知