Greedy:Linear world(POJ 2674)
2016-01-26 19:33
447 查看
Linear world
题目大意:一些人生活在线性世界中,到达线性世界两端就会消失,两个人的前进方向有两个,相遇会改变各自相遇方向,求最后一个人掉下的人的名字和时间。
其实这一题就是弹性碰撞的模型,所谓弹性碰撞的模型是两个物体相碰后会改变方向,但是可以看成是各自擦身而过,这个模型可以很快速求解与端点的问题
但是这一题还问你一个问题,就是你要找到这个人的名字,这个我一开始,没有想到,只能参考一下别人的代码了
/article/6468404.html
人的名字坐标 = 最晚消失对应人的坐标+这个人的前进方向的所在的与此人前进方向相反的人的个数
现在的问题是:为什么可以这么找?
其实我们可以这样看,假设只有一个P,则最晚消失的人将不会受到任何改变。
我们可以从上图中发现,设时间最晚消失的人一开始是P方向,如果不存在N方向,则最晚消失的人就是最晚消失对应位置,如果不存在P,只存在N,则最晚消失对应的人要往前进一个坐标,如果有两个则前进两个,当P和N同时存在,结论同时成立。所以我们只用找与前进方向相反的人的个数,就可以找到最晚消失的那个人的位置。
这个应该是数论问题,但是想了很久还是没有想到有什么好的解释,以后找到就更新一下
#include <iostream> #include <algorithm> #include <functional> #include <math.h> using namespace std; struct _set { bool dir; double pos; char name[260]; bool operator < (const _set&x)const { return pos < x.pos; } }inhabitants[32001], exchange[32001]; int Search_Name(const int, const int); void Merge_Sort(const int, const int); void Merge(const int, const int, const int); int main(void) { //最大距离位置距离方向有关 double length, rate, max_time; int sum_people, max_pos, last_stop_pos; char tmp_dir[2]; while (~scanf("%d", &sum_people)) { if (sum_people == 0) break; scanf("%lf%lf", &length, &rate); getchar(); for (int i = 0; i < sum_people; i++) { scanf("%s %lf %s", tmp_dir, &inhabitants[i].pos, inhabitants[i].name); inhabitants[i].dir = tmp_dir[0] == 'p' || tmp_dir[0] == 'P' ? 1 : 0; } sort(inhabitants, inhabitants + sum_people); //Merge_Sort(0, sum_people - 1); max_time = -1; for (int i = 0; i < sum_people; i++)//找到最大的位置 { if (inhabitants[i].dir == 1) { if ((length - inhabitants[i].pos) / rate> max_time)//正方向 { max_time = (length - inhabitants[i].pos) / rate; max_pos = i; } } else { if (inhabitants[i].pos / rate > max_time)//反方向 { max_time = inhabitants[i].pos / rate; max_pos = i; } } } last_stop_pos = Search_Name(max_pos, sum_people); printf("%13.2lf %s\n", floor(100 * max_time) / 100.0, inhabitants[last_stop_pos].name); } return EXIT_SUCCESS; } int Search_Name(const int max_pos,const int sum_people) { int dir_count = 0; if (inhabitants[max_pos].dir == 1) { for (int i = max_pos + 1; i < sum_people; i++) if (inhabitants[i].dir == 0) dir_count++; return max_pos + dir_count; } else { for (int i = 0; i < max_pos; i++) if (inhabitants[i].dir == 1) dir_count++; return max_pos - dir_count; } } /*void Merge_Sort(const int left,const int right) { int mid = (left + right) / 2; if (right > left) { Merge_Sort(left, mid); Merge_Sort(mid + 1, right); Merge(left, mid + 1, right); } } void Merge(const int left, const int mid, const int right) { int pos1 = left, pos2 = mid, l_end = mid - 1, r_end = right, pos_exchange; for (pos_exchange = left; pos1 <= l_end && pos2 <= r_end;) { if (inhabitants[pos1].pos < inhabitants[pos2].pos) exchange[pos_exchange++] = inhabitants[pos1++]; else exchange[pos_exchange++] = inhabitants[pos2++]; } while (pos1 <= l_end) exchange[pos_exchange++] = inhabitants[pos1++]; while (pos2 <= r_end) exchange[pos_exchange++] = inhabitants[pos2++]; for (int i = left; i <= right; i++) inhabitants[i] = exchange[i]; }*/
最后很多人在讨论板说sort会卡时间,我自己试了一下觉得还可以啊,657ms刷掉3000ms的题,编了个Merge_Sort,直接变1200ms.....
然后就是这一题,我醉了,P还分大小写,一开始没看题,醉了,欧洲人都喜欢这样玩的吗?
相关文章推荐
- 【ZYNQ-7000开发之十】TGP+VDMA+HDMI搭建视频通路
- MR-5.MapReduce常见Joins方法
- C++拷贝构造函数详解
- Dynamic Web Module 3.0 requires Java 1.6 or newer
- <OpenGL>Introduction
- POJ3071-Football(概率DP+滚动数组)
- Android的一些编码规范和优化
- sqlserver2005T-SQL查询语句的逻辑查询处理1
- Qt之进程间通信(Windows消息)
- Service 与 Activity之间的通信新方式
- android进程间的通信方式AIDL
- Qt之进程间通信(Windows消息)
- java火车站售票系统之多线程并发访问
- hadoop 第一个程序 wordcount 详解
- Swift传值传的是什么
- 如何解决C/C++中stack overflow问题
- MR-5.MapReduce排序
- mysql-otp 驱动中设置utf8mb4
- 设计模式笔记
- MR-5.MapReduce分布式缓存(Distribute Cache)