弹性碰撞的题目一般都是指碰到就会掉转方向的一类题目,这里我们可以忽略掉头,仅仅看成擦肩而过,交换名字等等

题意:一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的名字。

思路:

  1. 如果只有一只蚂蚁,就可以计算了
  2. 但是如果题目很多只,这个时候就要用上弹性碰撞题目的解题技巧
  3. 到最远的那只,确定它前进的方 向
  4. 找到前进方向有多少只跟其方向相反(即交换了多少次名字)
  5. 最后交换名字的那一只就是ans

特别主要开始时,要通过position来区分往右还是左

解决题目的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <iomanip>
using namespace std;
#define maxn 32000+16 struct node {
double position;
string name;
bool operator< (const node other)const {
return abs(position) < abs((other.position));
}
}ih[maxn]; int main()
{
int n;
while (cin >> n && n)
{
double l, v;
cin >> l >> v;
for (int i = ; i < n; i++)
{
char pn;
cin >> pn >> ih[i].position >> ih[i].name;
if (pn == 'n' || pn == 'N')
{
ih[i].position = -ih[i].position;
}
}
sort(ih, ih + n);
//找到最远的点
double max_d = 0.0;
int id=;
bool right = true;
for (int i = ; i < n; i++)
{
double ps = (ih[i].position < 0.0 ? : l) - ih[i].position;
if (max_d < ps)
{
max_d = ps;
id = i;
right = ih[i].position > 0.0;
}
}
//与这个点方向相反的点有
int count = ;
if (right)
{
for (int i = id; i < n; i++)
{
if (ih[i].position < 0.0)
count++;
}
id += count;
}
else {
for (int i = id; i>=; i--)
{
if (ih[i].position > 0.0)
count++;
}
id -= count;
}
double result = max_d / v;
cout << setw() << fixed << setprecision() << (int)(result * ) / 100.0 << ' ' << ih[id].name << endl;
}
return ;
}

poj 2674 线性世界 弹性碰撞的更多相关文章

  1. POJ 2674 Linear world(弹性碰撞)

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

  2. Greedy:Linear world(POJ 2674)

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

  3. POJ 2674 Linear world

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

  4. POJ 1745 线性和差取余判断

    POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...

  5. poj 3684 Physics Experiment 弹性碰撞

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

  6. POJ 3661 (线性DP)

    题目链接: http://poj.org/problem?id=3661 题目大意:牛跑步.有N分钟,M疲劳值.每分钟跑的距离不同.每分钟可以选择跑步或是休息.一旦休息了必须休息到疲劳值为0.0疲劳值 ...

  7. POJ 2674

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

  8. Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

    题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...

  9. Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

    题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #inc ...

随机推荐

  1. 《java提高数据导入效率优化思路》

    写在前边的实现需求: 1.总共10万个电话号码: 2.电话号码中有重复和错误: 3.查找出正确的号码(不重复): 一.优化前的实现方式: 1.先用正则过滤一遍10万条数据,找出错误的: 2.用List ...

  2. Android GreenDAO 3.0 不修改版本号的情况下增加、删除表、添加字段

    最近项目中使用了GreenDAO的3.0以上的版本,出现需要增加删除表的需求,刚开始用,发现官方对增加和删除的方法是每次去修改数据库版本号,版本一旦升级,那么原来数据库中的表会全部删除再重建.太麻烦, ...

  3. python模块详解 random os

    random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...

  4. ubuntu & sublime字体设置

    ubuntu # yahei http://pan.baidu.com/share/link?shareid=972621198&uk=1243888096&fid=333591974 ...

  5. sublim的正则匹配(待续)

    ctrl+H 打开匹配模式 打开正则匹配模式 正则匹配的一些方法:  点代表的是任意字符.* 代表的是取 0 至 无限长度问号代表的是非贪婪模式.三个链接在一起是取尽量少的任意字符,一般不会这么单独写 ...

  6. IOS transform的使用(移动,放大,旋转)

    @interface ViewController () - (IBAction)up; - (IBAction)big ; - (IBAction)leftRotate ; @property (n ...

  7. PHP设计模式练习——制作简单的投诉页面

    ---恢复内容开始--- <?php /* * 设计模式练习 * 1.数据库连接类(单例模式) * 2.调用接口实现留言本功能(工厂模式) * 3.实现分级举报处理功能(责任链模式) * 4.发 ...

  8. lasagne保存网络参数

    # Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...

  9. python :编写装饰器

    简单装饰器 def log_time(func): # 此函数的作用时接受被修饰的函数的引用test,然后被内部函数使用 def make_decorater(): print('现在开始装饰') f ...

  10. 单源最短路模板(dijkstra)

    单源最短路(dijkstra算法及堆优化) 弱化版题目链接 n^2 dijkstra模板 #include<iostream> #include<cstdio> #includ ...