POJ

来源:互联网 发布:手机网络劫持怎么办 编辑:程序博客网 时间:2024/06/05 10:10

传送门:POJ2674

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

思路:POJ1852升级版,不了解弹性碰撞的可以先去做那个题,这个题难点在于如何求最后掉下去的蚂蚁的名字,

我们可以认为蚂蚁相遇后名字会互相交换,并且擦肩而过,假设行进时间最长的蚂蚁为A,我们只需要关注在A的行进方向上有多少个和A反向的,就能知道会有多少只蚂蚁和真正的A碰头,但其实真正和A碰头的并不是最初和A反向的那些,而是在A初始方向上最靠近A的那几只蚂蚁,至于最终真正的A会带着谁的名字,只需要求出A的初始方向上有多少个初始方向和A反向的蚂蚁就行了,假设有cnt只,那么真正的A最后会携带A的初始方向上从A开始往前数cnt个的那只蚂蚁的名字。

说起来有点绕口,画画图就明白了。

代码:

#include<stdio.h>#include<iostream>#include<algorithm>#define ll long long#define pb push_back#define fi first#define se second#define pi acos(-1)#define inf 0x3f3f3f3f#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define rep(i,x,n) for(int i=x;i<n;i++)#define per(i,n,x) for(int i=n;i>=x;i--)using namespace std;typedef pair<int,int>P;const int MAXN=100010;int gcd(int a,int b){return b?gcd(b,a%b):a;}struct node{char dir;double pos;char name[255];bool operator < (const node &x) const{return pos < x.pos;}}p[32005];int main(){int n;while(cin >> n && n){double l, v;cin >> l >> v;for(int i = 0; i < n; i++){cin >> p[i].dir >> p[i].pos >> p[i].name; }//cout << p[0].name << endl;sort(p, p + n);int id = 0;bool flag = 1;//要是将其初始化为0就会wa,我也不知道为什么,但我想骂人double mmax = 0;for(int i = 0; i < n; i++){if(p[i].dir == 'n' || p[i].dir == 'N'){if(p[i].pos > mmax){mmax = p[i].pos;id = i;flag = 0;}}else{if(l - p[i].pos > mmax){mmax = l - p[i].pos;id = i;flag = 1;}}}int cnt = 0;if(flag){for(int i = id; i < n; i++)if(p[i].dir == 'n' || p[i].dir == 'N')cnt++;id += cnt;}else{for(int i = id; i >= 0; i--)if(p[i].dir == 'p' || p[i].dir == 'P')cnt++;id -= cnt;}mmax /= v;printf("%13.2lf %s\n", int(mmax * 100) / 100.0, p[id].name); } return 0;}