Codeforces Round #386 (Div. 2) C. Tram 数学、讨论
2017-01-11 01:06
489 查看
C. Tram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The tram in Berland goes along a straight line from the point 0 to the point s and
back, passing 1 meter per t1 seconds
in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.
Igor is at the point x1.
He should reach the point x2.
Igor passes 1 meter per t2 seconds.
Your task is to determine the minimum time Igor needs to get from the point x1 to
the point x2,
if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x1.
Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that 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 than 1 meter per t2 seconds).
He can also stand at some point for some time.
Input
The first line contains three integers s, x1 and x2 (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.
The second line contains two integers t1 and t2 (1 ≤ t1, t2 ≤ 1000) —
the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.
The third line contains two integers p and d (1 ≤ p ≤ s - 1, d is
either 1 or
) —
the position of the tram in the moment Igor came to the point x1 and
the direction of the tram at this moment. If
,
the tram goes in the direction from the point s to the point 0.
Ifd = 1, the tram goes in the direction from the point 0 to
the point s.
Output
Print the minimum time in seconds which Igor needs to get from the point x1 to
the point x2.
Examples
input
output
input
output
Note
In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds
in total, because he passes 1 meter per 4 seconds.
In the second example Igor can, for example, go towards the point x2 and
get to the point 1 in 6 seconds
(because he has to pass 3meters, but he passes 1 meters
per 2 seconds). At that moment the tram will be at the point 1,
so Igor can enter the tram and pass 1meter in 1 second.
Thus, Igor will reach the point x2 in 7 seconds
in total.
Source
Codeforces Round #386 (Div. 2)
My Solution
题意:从x1 出发到 x2,走路速度是t2 s/ 单位长度 ,坐车是t1 s/ 单位长度,车子在0~s间不断往返,此时车在p位置,且方向是d,(正向 d == 1,反向 d == -1) 问从x1到x2的最短时间
数学、讨论
此外题可以分成2大类,到达的时候是走路的则只考虑全走路,如果到达的时候是坐车则只考虑坐车,
即x1到达x2所花的时间为此时p位置到最后以x1指向x2的方向经过x2时的总时间。
具体分成如下6类讨论
当 x1 < x2 时
ans = (x2 - x1) * t2;
if(d == 1){
if(p <= x1){ //p 在 x1、 x2 左边边
ans = min(ans, (x2 - p) * t1);
}
else{
ans = min(ans, (s + x2 + s - p) * t1);
}
}
else{
ans = min(ans, (p + x2) * t1);
}
当 x1 > x2 时
ans = (x1 - x2) * t2;
if(d == 1){
ans = min(ans, (s - p + s - x2) * t1); //
}
else{
if(p >= x1){ //p 在 x1、 x2 右边
ans = min(ans, (p - x2) * t1);
}
else{
ans = min(ans, (p + s + s - x2) * t1);
}
}
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
int main()
{
#ifdef LOCAL
freopen("c.txt", "r", stdin);
//freopen("c.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
LL s, x1, x2, t1, t2, p, d, ans = 9e18;
cin >> s >> x1 >> x2;
cin >> t1 >> t2;
cin >> p >> d;
if(x1 < x2){
ans = (x2 - x1) * t2;
if(d == 1){
if(p <= x1){
ans = min(ans, (x2 - p) * t1);
}
else{
ans = min(ans, (s + x2 + s - p) * t1);
}
}
else{
ans = min(ans, (p + x2) * t1);
}
}
else{
ans = (x1 - x2) * t2;
if(d == 1){
ans = min(ans, (s - p + s - x2) * t1); //(©Ð£ß©Ð)
}
else{
if(p >= x1){
ans = min(ans, (p - x2) * t1);
}
else{
ans = min(ans, (p + s + s - x2) * t1);
}
}
}
cout << ans << endl;
#ifdef LOCAL
cout << endl;
}
#endif ** LOCAL
return 0;
}
Thank you!
------from ProLights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The tram in Berland goes along a straight line from the point 0 to the point s and
back, passing 1 meter per t1 seconds
in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.
Igor is at the point x1.
He should reach the point x2.
Igor passes 1 meter per t2 seconds.
Your task is to determine the minimum time Igor needs to get from the point x1 to
the point x2,
if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x1.
Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that 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 than 1 meter per t2 seconds).
He can also stand at some point for some time.
Input
The first line contains three integers s, x1 and x2 (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.
The second line contains two integers t1 and t2 (1 ≤ t1, t2 ≤ 1000) —
the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.
The third line contains two integers p and d (1 ≤ p ≤ s - 1, d is
either 1 or
) —
the position of the tram in the moment Igor came to the point x1 and
the direction of the tram at this moment. If
,
the tram goes in the direction from the point s to the point 0.
Ifd = 1, the tram goes in the direction from the point 0 to
the point s.
Output
Print the minimum time in seconds which Igor needs to get from the point x1 to
the point x2.
Examples
input
4 2 4 3 4 1 1
output
8
input
5 4 0 1 2 3 1
output
7
Note
In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds
in total, because he passes 1 meter per 4 seconds.
In the second example Igor can, for example, go towards the point x2 and
get to the point 1 in 6 seconds
(because he has to pass 3meters, but he passes 1 meters
per 2 seconds). At that moment the tram will be at the point 1,
so Igor can enter the tram and pass 1meter in 1 second.
Thus, Igor will reach the point x2 in 7 seconds
in total.
Source
Codeforces Round #386 (Div. 2)
My Solution
题意:从x1 出发到 x2,走路速度是t2 s/ 单位长度 ,坐车是t1 s/ 单位长度,车子在0~s间不断往返,此时车在p位置,且方向是d,(正向 d == 1,反向 d == -1) 问从x1到x2的最短时间
数学、讨论
此外题可以分成2大类,到达的时候是走路的则只考虑全走路,如果到达的时候是坐车则只考虑坐车,
即x1到达x2所花的时间为此时p位置到最后以x1指向x2的方向经过x2时的总时间。
具体分成如下6类讨论
当 x1 < x2 时
ans = (x2 - x1) * t2;
if(d == 1){
if(p <= x1){ //p 在 x1、 x2 左边边
ans = min(ans, (x2 - p) * t1);
}
else{
ans = min(ans, (s + x2 + s - p) * t1);
}
}
else{
ans = min(ans, (p + x2) * t1);
}
当 x1 > x2 时
ans = (x1 - x2) * t2;
if(d == 1){
ans = min(ans, (s - p + s - x2) * t1); //
}
else{
if(p >= x1){ //p 在 x1、 x2 右边
ans = min(ans, (p - x2) * t1);
}
else{
ans = min(ans, (p + s + s - x2) * t1);
}
}
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
int main()
{
#ifdef LOCAL
freopen("c.txt", "r", stdin);
//freopen("c.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
LL s, x1, x2, t1, t2, p, d, ans = 9e18;
cin >> s >> x1 >> x2;
cin >> t1 >> t2;
cin >> p >> d;
if(x1 < x2){
ans = (x2 - x1) * t2;
if(d == 1){
if(p <= x1){
ans = min(ans, (x2 - p) * t1);
}
else{
ans = min(ans, (s + x2 + s - p) * t1);
}
}
else{
ans = min(ans, (p + x2) * t1);
}
}
else{
ans = (x1 - x2) * t2;
if(d == 1){
ans = min(ans, (s - p + s - x2) * t1); //(©Ð£ß©Ð)
}
else{
if(p >= x1){
ans = min(ans, (p - x2) * t1);
}
else{
ans = min(ans, (p + s + s - x2) * t1);
}
}
}
cout << ans << endl;
#ifdef LOCAL
cout << endl;
}
#endif ** LOCAL
return 0;
}
Thank you!
------from ProLights
相关文章推荐
- 反射注解浅引
- php7-nginx-composer 安装laravel
- Shiro-从数据表中初始化资源和权限
- mongodb基本概念
- Ubuntu系统下解决SublimeText3中文输入
- 关于Linux下的密码输入*的问题
- 华为云使用初体验
- (找数字)HDU - 2141
- 洛谷 p1044 栈 对递归的感觉又加深了
- github常用操作
- github常用操作
- dns服务搭建
- dns服务搭建
- Leetcode Merge Intervals
- SVN与eclipse集成安装步骤_百度经验
- [24]CSS3 弹性伸缩布局(上)
- pip 更改源 pip加速
- 分析分布式服务框架
- CentOS 6.5 Install Oracle Java 8
- 【机器学习】线性回归