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

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

思路:

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

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

解决题目的代码:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <cstring>
  6. #include <vector>
  7. #include <queue>
  8. #include <string>
  9. #include <iomanip>
  10. using namespace std;
  11. #define maxn 32000+16
  12.  
  13. struct node {
  14. double position;
  15. string name;
  16. bool operator< (const node other)const {
  17. return abs(position) < abs((other.position));
  18. }
  19. }ih[maxn];
  20.  
  21. int main()
  22. {
  23. int n;
  24. while (cin >> n && n)
  25. {
  26. double l, v;
  27. cin >> l >> v;
  28. for (int i = ; i < n; i++)
  29. {
  30. char pn;
  31. cin >> pn >> ih[i].position >> ih[i].name;
  32. if (pn == 'n' || pn == 'N')
  33. {
  34. ih[i].position = -ih[i].position;
  35. }
  36. }
  37. sort(ih, ih + n);
  38. //找到最远的点
  39. double max_d = 0.0;
  40. int id=;
  41. bool right = true;
  42. for (int i = ; i < n; i++)
  43. {
  44. double ps = (ih[i].position < 0.0 ? : l) - ih[i].position;
  45. if (max_d < ps)
  46. {
  47. max_d = ps;
  48. id = i;
  49. right = ih[i].position > 0.0;
  50. }
  51. }
  52. //与这个点方向相反的点有
  53. int count = ;
  54. if (right)
  55. {
  56. for (int i = id; i < n; i++)
  57. {
  58. if (ih[i].position < 0.0)
  59. count++;
  60. }
  61. id += count;
  62. }
  63. else {
  64. for (int i = id; i>=; i--)
  65. {
  66. if (ih[i].position > 0.0)
  67. count++;
  68. }
  69. id -= count;
  70. }
  71. double result = max_d / v;
  72. cout << setw() << fixed << setprecision() << (int)(result * ) / 100.0 << ' ' << ih[id].name << endl;
  73. }
  74. return ;
  75. }

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. 高仿饿了么mock本地数据

    未使用router新版webpack.dev.conf.js配置本地数据访问:// 引入express 模块 const express = require('express') // 创建expre ...

  2. 修改Android系统关机动画

    文件路径:frameworks\base\services\core\java\com\android\server\power\ShutdownThread.java 在beginShutdownS ...

  3. C#或Net连接Oracle操作提示 Oracle 客户端 version 8.1.7 或更高版本报错

    异常问题 远程连接ORCALE 服务器,而本地未安装任何ORCALE 相关客户端及ODAC .using System.Data.OracleClient;当我们用程序操作Oracle库的时候,OPE ...

  4. So you want to write a desktop app in Python

    So you want to write a desktop app in Python Thomas Kluyver 2014-06-16 23:55 51 Comments Source This ...

  5. phpstorm 2017.1 激活

    打开网址 http://idea.lanyus.com/ 选择获取注册码,复制生成的验证码 安装完成后,打开软件,依次选择菜单栏 Help -> Register-> Activation ...

  6. 笨办法学Python(三十六)

    习题 36: 设计和调试 现在你已经学会了“if 语句”,我将给你一些使用“for 循环”和“while 循环”的规则,一面你日后碰到麻烦.我还会教你一些调试的小技巧,以便你能发现自己程序的问题.最后 ...

  7. Selenium入门10 弹出框的处理 switch_to.alert

      三种弹出框alert(一个按钮),confirm(两个确认,取消),prompt(两个按钮+输入框). 切换到弹框: switch_to_alert()  新版的selenium用:  brows ...

  8. Selenium入门系列4 选择并操作一组元素

    选中一组元素的方式也是8种,与选中单个元素一一对应.区别只在于element与elements.elements取到的是一个数组,element取符合条件的第一个元素. 首先在脚本的目录下新建test ...

  9. Radix Sort

    为了完成二维数据快速分类,最先使用的是hash分类. 前几天我突然想,既然基数排序的时间复杂度也不高,而且可能比hash分类更稳定,所以不妨试一下. 在实现上我依次实现: 1.一维数组基数排序 基本解 ...

  10. Maven安装以及使用

    maven安装 1.解压maven安装包 2.右键“计算机”,选择“属性”,之后点击“高级系统设置”,点击“环境变量”,来设置环境变量. 3. 新建系统变量 MAVEN_HOME 变量值:C:\apa ...