POJ 2674 Linear world

POJ 2674 Linear world

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

   注意两点: 相撞可视为擦肩而过,蚂蚁们不管掉不掉头它们的相对位置保持不变

   这个题是大白上原来的蚂蚁相撞问题(POJ 1852)的加强版

    如果只考虑最后掉下去那只蚂蚁的时间(即蚂蚁全部掉下的最长时间),那么可以认为这些蚂蚁相撞时直接擦肩而过.

    但是该题还要求出最后掉下去那只蚂蚁的名字,这样看起来似乎要考虑每个蚂蚁相撞的情况,若记录获得最大值的蚂蚁为A,若A和B碰撞,之后B又和C碰撞,之后C又和D.... 的最后一个人,这样最后一个相撞的人的名字即为所求,但是这样就不好处理

    如果把相撞视为擦肩而过,让蚂蚁们按原方向一直走,每只蚂蚁走pos+maxn或者pos-maxn后,此时对这些距离从小到大排序,找到位置为0或者L的点,记起下标为temp,由于蚂蚁的相对位置保持不变,则ant[pos].name即为所求.

    注意最后是要截取两位小数,不是四舍五入,所以要用floor(maxn/V*100)/100.0(因为这个wa了几次)

/*
* Created:     2016年04月06日 10时29分37秒 星期三
* Author:      Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define in3(x1,x2,x3) scanf("%d%d%d",&(x1),&(x2),&(x3))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d
",(x))
#define out2(x1,x2) printf("%d %d
",(x1),(x2))
#define outf(x) printf("%f
",(x))
#define outlf(x) printf("%lf
",(x))
#define outlf2(x1,x2) printf("%lf %lf
",(x1),(x2));
#define outll(x) printf("%I64d
",(x))
#define outlld(x) printf("%lld
",(x))
#define outc(str) printf("%c
",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true;

struct node{
    char dir[4]; //方向
    double pos;//位置
    char name[255];
};
node ant[32000];
double p[32000];
int n;
double L,V;
bool cmp(node x,node y){
    return x.pos<y.pos;
}
void solve(){
    double maxn,dis;
    maxn=0;
    rep(i,0,n){
        dis=(ant[i].dir[0]=='P'||ant[i].dir[0]=='p')?L:0;
        maxn=max(maxn,abs(dis-ant[i].pos));
    }
    rep(i,0,n){
        dis=(ant[i].dir[0]=='P'||ant[i].dir[0]=='p')?maxn:-maxn;
        p[i]=dis+ant[i].pos;
    }
    sort(p,p+n);
    int temp;
    rep(i,0,n){
        if(p[i]==0||p[i]==L){
        temp=i;break;
        }
    }
    printf("%13.2f %s
",floor(maxn/V*100)/100.0,ant[temp].name);
}
int main()
{
    while(in(n)&&n){
        inlf2(L,V);
        rep(i,0,n){
            scanf("%s %lf %s",&ant[i].dir,&ant[i].pos,&ant[i].name);
        }
        solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/akrusher/p/5358514.html