题意:
在一条线上,告诉你每个蚂蚁的位置和方向,两只蚂蚁碰面后原路返回,告诉你线的长度,和蚂蚁速度(所有的蚂蚁速度都一样),求最后一只蚂蚁掉落的时间和名字。
思路:
很久之前做的一道思路题目了。 这个题比较坑把 记录一下。
两只蚂蚁碰面后相当于不回头一直往前走。
那么我们可以记录下来每只蚂蚁假如不碰面掉落的时间,取个最大值就是最后一只蚂蚁掉落的时间,在把那个时间对应的每只蚂蚁位置记录下来,还在线上的就是最后一只蚂蚁。
坑:
竟然是截断到两位小数,不是四舍五入,那么直接给ans×100,向下取整后在除以100即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#define Siz(x) (int)x.size()
using namespace std;
const int maxn = 320000 + 7;
const double eps = 1e-10;
int dcmp(double a,double b){
if (fabs(a-b) < eps) return 0;
if (a > b) return 1;
return -1;
}
char s[320];
struct Node{
char name[320];
double pos;int dir;
void read(){
scanf("%s",s);
if (s[0] == 'p' || s[0] == 'P')dir = 1;
else dir = -1;
scanf("%lf",&pos);
scanf("%s",name);
}
}p[maxn];
vector<double>v;
int main(){
double L,t;
int n;
while(~scanf("%d",&n) && n){
scanf("%lf %lf",&L, &t);
v.clear();
double Maxtime = -1;
for (int i = 0; i < n; ++i){
p[i].read();
double pos = p[i].pos;
int dir = p[i].dir;
if (dir == 1){
if ((L-pos)/t > Maxtime)Maxtime = (L-pos)/t;
}
else {
if (pos/t >Maxtime)Maxtime=pos/t;
}
}
int ans;
for (int i = 0; i < n; ++i){
double pos = p[i].pos;
int dir = p[i].dir;
double p2 = pos+dir*t*Maxtime;
v.push_back(p2);
}
sort(v.begin(),v.end());
for (int i = 0; i < Siz(v); ++i){
double p2 = v[i];
if (dcmp(p2,0) >= 0 && dcmp(p2,L) <= 0){
ans= i;
break;
}
}
Maxtime*=100;
Maxtime = (int)Maxtime *1.0;
Maxtime/=100;
printf("%13.2f %s\n",Maxtime,p[ans].name);
}
return 0;
}
/**
1
13.5 2
p 3.5 Smarty
4
10 1
p 1 Helga
n 3 Joanna
p 5 Venus
n 7 Clever
0
5.00
**/
Linear world Description The Disc, being flat, has no real horizon. Any adventurous sailors who get funny ideas from staring at eggs and oranges for too long and set out for the antipodes soon learned that the reason why distant ships sometimes looked as though they were disappearing over the edge of the world was that they were disappearing over the edge of the world. (Terry Pratchett -Colour of Magic) Input The input consists of multiple descriptions (data sets) of the creation moment. File structure is as follows: Output The output consists of one line per each input data set. The first value should be the time when the last inhabitant will fall of the linear world counting from the moment of creation. Value should be printed truncated to two decimal places in a field 13 characters wide. The second value should be the name of the inhabitant. Values should be separated with single space character. Sample Input 1 13.5 2 p 3.5 Smarty 4 10 1 p 1 Helga n 3 Joanna p 5 Venus n 7 Clever 0 Sample Output 5.00 Smarty 9.00 Venus Source Southeastern Europe 2005 |
Time Limit: 3000MS | | Memory Limit: 65536K |
Total Submissions: 3633 | | Accepted: 813 |
[Submit] [Go Back] [Status] [Discuss]
Home Page
Go Back
To top
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator