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].
 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
struct node
{
char d[];
double dec;
char name[];
} c[];
bool cmp(const node& a,const node& b)
{
return a.dec<b.dec;
}
int main()
{
int n,i,j;
double L,V;
while(~scanf("%d",&n))
{
if(!n)break;
scanf("%lf%lf",&L,&V);
for(i=; i<=n; i++)
{
scanf("%s %lf %s",&c[i].d,&c[i].dec,&c[i].name);
}
sort(c+,c+n+,cmp);
int id;
double maL=;
for(i=; i<=n; i++)
{
double R;
if(c[i].d[]=='p'||c[i].d[]=='P')
R=L-c[i].dec;
else R=c[i].dec;
if(R>maL)
{
maL=R;
id=i;
}
}
int ans=;
if(c[id].d[]=='p'||c[id].d[]=='P')
{
for(j=id+; j<=n; j++)
if(c[j].d[]=='n'||c[j].d[]=='N')
ans++;
ans=id+ans;
}
else
{
for(j=id-; j>=; j--)
if(c[j].d[]=='p'||c[j].d[]=='P')
ans++;
ans=id-ans;
}
maL=maL/V;
printf("%13.2lf %s\n",floor(maL*)/100.00,c[ans].name);
}
return ;
}

思路:

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

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

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

 #include <iostream>
#include <cstdio>
using namespace std; const int N = ;
char name[N][]; int main()
{
int n;
double l, v; while (cin >> n && n)
{
cin >> l >> v;
int pnum = ;
double pmaxt = -, nmaxt = -;
char dir[];
double pos;
for (int i = ; i < n; i++)
{
scanf("%s%lf%s", dir, &pos, name[i]);
if (dir[] == 'p' || dir[] == 'P')
{
pnum++;
if (pmaxt < )//第一个人(最大的pmax)
pmaxt = (l - pos) / v;
}
else
{
nmaxt = pos / v;//最后一个人(最大的nmax)
}
}
//结果小数点2位之后的数是直接截断的,并不是四舍五入,因此不可以直接打印,要先处理再打印
if (pmaxt > nmaxt)
printf("%13.2lf %s\n", (int)(pmaxt * ) / 100.0, name[n - pnum]);
else
printf("%13.2lf %s\n", (int)(nmaxt * ) / 100.0, name[n - pnum - ]);
}
return ;
}

POJ 2674 Linear world(弹性碰撞)的更多相关文章

  1. POJ 2674 Linear world

    POJ 2674 Linear world 题目大意: 一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的时间和名字. 注意两点: 相撞可视为擦肩而过,蚂蚁们不管掉 ...

  2. poj 2674 线性世界 弹性碰撞

    弹性碰撞的题目一般都是指碰到就会掉转方向的一类题目,这里我们可以忽略掉头,仅仅看成擦肩而过,交换名字等等 题意:一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的名 ...

  3. Greedy:Linear world(POJ 2674)

      Linear world 题目大意:一些人生活在线性世界中,到达线性世界两端就会消失,两个人的前进方向有两个,相遇会改变各自相遇方向,求最后一个人掉下的人的名字和时间. 其实这一题就是弹性碰撞的模 ...

  4. POJ 2674

    Linear world Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2448   Accepted: 564 Descr ...

  5. poj 3684 Physics Experiment 弹性碰撞

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1489   Accepted: 509 ...

  6. POJ:3684-Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3392 Accepted: 1177 Sp ...

  7. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  8. POJ 3684 Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2936   Accepted: 104 ...

  9. Greedy:Physics Experiment(弹性碰撞模型)(POJ 3848)

    物理实验 题目大意:有一个与地面垂直的管子,管口与地面相距H,管子里面有很多弹性球,从t=0时,第一个球从管口求开始下落,然后每1s就会又有球从球当前位置开始下落,球碰到地面原速返回,球与球之间相碰会 ...

随机推荐

  1. SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope

    @Scope的使用很简单,直接在类上加上就行 @PostConstruct:相当于xml配置方式的init-method方法 @PreDestroy:相当于xml配置方式的destroy-method ...

  2. Composer 安装东西遇到github需要token怎么办

    安装yii2遇到这样的提示: Could not fetch https://api.github.com/repos/jquery/sizzle/contents/bower.json?ref=91 ...

  3. linux centos 安装opencv

    系统:Centos 6.5 最后版本 OpenCV: 2.4.9 1.安装依赖包(很重要) yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gi ...

  4. iOS如何限制使用SDK的版本? 解决iOS项目的版本兼容问题

      更新 2015-11-16 感谢微博好友@zyyy_000的评论,补充了为什么要在+ (void)load方法里面做Method Swizzling. 前言 最近,在做项目时,因为某种原因,突然要 ...

  5. LOJ2360. 「NOIP2016」换教室【概率DP】【Floyed】【傻逼题】

    LINK 思路 先floyed出两点最短路 然后就可以直接\(dp_{i,j,0/1}\)表示前i节课选择换j节,换不换当前这一节的最小贡献 直接可以枚举上一次决策的状态计算概率进行统计就可以了 我变 ...

  6. BZOJ2431 HAOI2009 逆序对数列 【DP】*

    BZOJ2431 HAOI2009 逆序对数列 Description 对于一个数列ai{a_i}ai​,如果有i<j且ai>aja_i>a_jai​>aj​,那么我们称aia ...

  7. LOJ2611. NOIP2013 积木大赛 【线段树】

    LOJ2611. NOIP2013 积木大赛 LINK 题目大意是给你一个目标状态数组 每次你可以选择一个连续区间加上一个值,求最小操作次数 我是神奇的脑子 最近做数据结构疯了 然后看见这题就数据结构 ...

  8. BZOJ3932 CQOI2015 任务查询系统 【主席树】

    BZOJ3932 CQOI2015 任务查询系统 Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei, ...

  9. Oracle中用exp/imp命令快速导入导出数据

    from: http://blog.csdn.net/wangchunyu11155/article/details/53635602 [用 exp 数 据 导 出]: 1 将数据库TEST完全导出, ...

  10. Numpy, Pandas, Matplotlib, Scipy 初步

    Numpy: 计算基础,  以类似于matlab的矩阵计算为基础.  底层以C实现, 速度快. Pandas: 以numpy为基础, 扩充了很多统计工具. 重点是数据统计分析. Matplotlib: ...