poj之旅——2674
来源:互联网 发布:.net在线考试系统源码 编辑:程序博客网 时间:2024/04/29 15:51
题目描述:在长度为L的杆上,有n只蚂蚁,他们有的向右爬,有的向左爬,速度均为v,求最后一只掉下杆子的蚂蚁的时间与名称。
题解:时间显而易见,maxdistance/v即可。
名称有点麻烦,需要先保存距离为maxdistance的蚂蚁编号,再沿着它的方向,看有几只蚂蚁和他方向相反,编号对应加减一下即可。
参考程序:
#include<cstdio>#include<algorithm>#include<cmath>#define maxn 33000#define EPS 1e-6using namespace std;struct Ant{double pos;char name[256];bool operator < (const Ant& rhs)const {return fabs(pos)<fabs(rhs.pos);}}a[maxn];int main(){int n;while (scanf("%d",&n)==1 && n){double L;int v;scanf("%lf%d",&L,&v);for (int i=0;i<n;i++){char dire;scanf("\n%c %lf %s",&dire,&a[i].pos,&a[i].name);a[i].pos*=(dire=='n' || dire=='N')?-1:1;}sort(a,a+n);double maxd=0.0;int id=0;bool dir=true;for (int i=0;i<n;i++){double d=(a[i].pos<0.0?0:L)-a[i].pos;if (d>maxd+EPS){maxd=d;id=i;dir=a[i].pos>0.0;}}int cnt=0;if (dir){for (int i=id;i<n;i++)if (a[i].pos<0.0)cnt++;id+=cnt;}else{for (int i=id;i>=0;i--)if (a[i].pos>0)cnt++;id-=cnt;}double res=maxd/v;printf("%13.2f %s\n",floor(res*100)/100.0,a[id].name);}return 0;}
0 0
- poj之旅——2674
- poj之旅——3258
- poj之旅——3273
- poj之旅——3104
- poj之旅——3045
- poj之旅——2976
- poj之旅——3111。
- poj之旅——3579
- poj之旅——1979
- poj之旅——3009
- poj之旅——3669
- poj之旅——2718
- poj之旅——3187
- poj之旅——3050
- poj之旅——2376
- poj之旅——1328
- poj之旅——3190
- poj之旅——2393
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转载)
- 脚本查询数据表结构返回,可扩展
- 产品经理进阶篇幅(1)
- 配图快速入门及地图性能优化(2)
- App级异常收集
- poj之旅——2674
- 关于APM飞控加风火轮机架四轴对角电机发热的问题(2)
- windows 下 安装 RabbitMQ 服务器 环境
- Leetcode-2Add Two Numbers
- C++面试题: main函数执行完之后还会调用其他的函数吗?
- mybatis的sql的xml的配置文件中<![CDATA[ ]]>的用法。
- log4j的rsyslog配置
- 在 Eclipse 中使用 JUnit4 进行单元测试(图文教程一)
- guava 常用对象方法