Codeforces 746 C. Tram的解题思路是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1097个文字,预计阅读时间需要5分钟。
C. 验证电车时间限制每测试内存限制每测试输入输出0至s点并返回,每秒行驶1米。这意味着电车始终处于匀速直线运动状态,可以瞬间转向。
C. Tram
time limit per test
memory limit per test
input
output
0to the pointsand back, passing1meter pert1seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at pointsx = 0andx = s.
x1. He should reach the pointx2. Igor passes1meter pert2
x1to the pointx2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the pointx1.
not obligatorythat points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than1meter pert2
Input
s,x1andx2(2 ≤ s ≤ 1000,0 ≤ x1, x2 ≤ s,x1 ≠ x2)— the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.
t1andt2(1 ≤ t1, t2)— the time in seconds in which the tram passes1meter and the time in seconds in which Igor passes1
pandd(1 ≤ p ≤ s - 1,dis either1or
)— the position of the tram in the moment Igor came to the pointx1and the direction of the tram at this moment. If
, the tram goes in the direction from the pointsto the point0. Ifd = 1, the tram goes in the direction from the point0to the points.
Output
x1to the pointx2.
Examples
input
4 2 43 4 1 1
output
8
input
5 4 01 2 3 1
output
7
Note
2meters and it takes8seconds in total, because he passes1meter per4
x2and get to the point1in6seconds (because he has to pass3meters, but he passes1meters per2seconds). At that moment the tram will be at the point1, so Igor can enter the tram and pass1meter in1second. Thus, Igor will reach the pointx2in7
/*************************************************************************
这个题第一次看想多了,看了别人的博客才知道这题的思路。
问人从x1处,如何最快到达x2 处。t1,t2分别表示车和人每走一米的时间。
先考虑一种情况:如果车速比人慢,就没有上车的必要了,以下讨论车速快于人的速度
一开始我猜想,人可能走一段后上车,那我们假设我这种猜想成立,那么人最后一定是在终点x2处下车,那么无论人从哪里上车,都是同样的时间下车(人上下车的时间是忽略不计的),所以我们干脆从一开始就不要步行,直接等车。
所以,我们只需要比较,人步行到达终点的时间,和车到达终点的时间,取其小的。
用暴力模拟一下这个过程即可。
/*****************
代码如下:
/**
#include<stdio.h>
int s,x1,x2,t1,t2,p,d;
int main()
{
while(scanf("%d%d%d%d%d%d%d",&s,&x1,&x2,&t1,&t2,&p,&d)!=EOF)
{
int time=(x2-x1)*t2;//步行所用的时间
if(time<0)
time=-time;
int timecar=0;
int pc=p; //pc表示此时此刻车的位置
int f1=0; //f1标记x1是否经过
while(1)
{
if(pc==s||pc==0)//如果车到了两端,那就换方向
d=-d;//转向
if(pc==x1)//经过起点,只有经过起点,才能让人上车
f1=1;
if(pc==x2 && f1==1)//经过终点,人下车
break;
pc+=d; //向当前方向走一米
timecar+=t1;//加上车,这一秒的用时
}
printf("%d\n",time<timecar ? time : timecar);//取时间短的输出
}
return 0;
}
本文共计1097个文字,预计阅读时间需要5分钟。
C. 验证电车时间限制每测试内存限制每测试输入输出0至s点并返回,每秒行驶1米。这意味着电车始终处于匀速直线运动状态,可以瞬间转向。
C. Tram
time limit per test
memory limit per test
input
output
0to the pointsand back, passing1meter pert1seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at pointsx = 0andx = s.
x1. He should reach the pointx2. Igor passes1meter pert2
x1to the pointx2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the pointx1.
not obligatorythat points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than1meter pert2
Input
s,x1andx2(2 ≤ s ≤ 1000,0 ≤ x1, x2 ≤ s,x1 ≠ x2)— the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.
t1andt2(1 ≤ t1, t2)— the time in seconds in which the tram passes1meter and the time in seconds in which Igor passes1
pandd(1 ≤ p ≤ s - 1,dis either1or
)— the position of the tram in the moment Igor came to the pointx1and the direction of the tram at this moment. If
, the tram goes in the direction from the pointsto the point0. Ifd = 1, the tram goes in the direction from the point0to the points.
Output
x1to the pointx2.
Examples
input
4 2 43 4 1 1
output
8
input
5 4 01 2 3 1
output
7
Note
2meters and it takes8seconds in total, because he passes1meter per4
x2and get to the point1in6seconds (because he has to pass3meters, but he passes1meters per2seconds). At that moment the tram will be at the point1, so Igor can enter the tram and pass1meter in1second. Thus, Igor will reach the pointx2in7
/*************************************************************************
这个题第一次看想多了,看了别人的博客才知道这题的思路。
问人从x1处,如何最快到达x2 处。t1,t2分别表示车和人每走一米的时间。
先考虑一种情况:如果车速比人慢,就没有上车的必要了,以下讨论车速快于人的速度
一开始我猜想,人可能走一段后上车,那我们假设我这种猜想成立,那么人最后一定是在终点x2处下车,那么无论人从哪里上车,都是同样的时间下车(人上下车的时间是忽略不计的),所以我们干脆从一开始就不要步行,直接等车。
所以,我们只需要比较,人步行到达终点的时间,和车到达终点的时间,取其小的。
用暴力模拟一下这个过程即可。
/*****************
代码如下:
/**
#include<stdio.h>
int s,x1,x2,t1,t2,p,d;
int main()
{
while(scanf("%d%d%d%d%d%d%d",&s,&x1,&x2,&t1,&t2,&p,&d)!=EOF)
{
int time=(x2-x1)*t2;//步行所用的时间
if(time<0)
time=-time;
int timecar=0;
int pc=p; //pc表示此时此刻车的位置
int f1=0; //f1标记x1是否经过
while(1)
{
if(pc==s||pc==0)//如果车到了两端,那就换方向
d=-d;//转向
if(pc==x1)//经过起点,只有经过起点,才能让人上车
f1=1;
if(pc==x2 && f1==1)//经过终点,人下车
break;
pc+=d; //向当前方向走一米
timecar+=t1;//加上车,这一秒的用时
}
printf("%d\n",time<timecar ? time : timecar);//取时间短的输出
}
return 0;
}

