【数学计算】#88 A. Elevator


A. Elevator
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All nparticipants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".

The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the m-th floor. After that the elevator moves to floor m - 1, then to floor m - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.

For each of the n participant you are given si, which represents the floor where the i-th participant starts, fi, which represents the floor the i-th participant wants to reach, and ti, which represents the time when the i-th participant starts on the floor si.

For each participant print the minimum time of his/her arrival to the floor fi.

If the elevator stops on the floor si at the time ti, then the i-th participant can enter the elevator immediately. If the participant starts on the floor si and that's the floor he wanted to reach initially (si = fi), then the time of arrival to the floor fi for this participant is considered equal to ti.

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 2 ≤ m ≤ 108).

Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 ≤ si, fi ≤ m, 0 ≤ ti ≤ 108), described in the problem statement.

Output

Print n lines each containing one integer — the time of the arrival for each participant to the required floor.

Sample test(s)
input
7 4
2 4 3
1 2 0
2 2 0
1 2 1
4 3 5
1 2 2
4 2 0
output
9
1
0
7
10
7
5
input
5 5
1 5 4
1 3 1
1 3 4
3 1 5
4 2 5
output
12
10
10
8
7
Note

Let's consider the first sample. The first participant starts at floor s = 2 by the time equal to t = 3. To get to the floor f = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor f at time equal to 9. The second participant starts at the time t = 0 on the floor s = 1, enters the elevator immediately, and arrives to the floor f = 2. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts.



一句话:哇塞题目好长!话说我这个专题可以作为……A题缩句者~

有一栋m层高的大楼,有一个电梯是从一楼开始,一层一层向上,到顶之后一层一层下降,这样循环往复的过程。

有n个人他们要搭电梯,每个人有3个参数: s是这个人的出发层,f是这个人的目标层,t是指这个人从t时刻开始在电梯口等待,问:输出每一个人到达目的地时的到达时间。

需要注意的是,电梯从底到顶是m-1次移动,所以一个往复是2*m-2,当一个人错过了当前这轮电梯的时候他将只有等待下一班的到来,结合生活实际就会容易理解一些。


闲着没事干代码也缩了个句……

Code:

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
// http://codeforces.com/contest/117
// Elevator

int main()
{
    int n,m,s,f,t;
    scanf("%d%d",&n,&m);
    for(m=m*2-2; n--; printf("%d\n",s^f?(t+m-(s>f?m+2-s:s))/m*m+(s>f?m+2-f:f)-1:t))
    scanf("%d%d%d",&s,&f,&t);
    return 0;
}

那个for是不是过分了点…… 异或的意思就是不等啦,据说计算起来比较快……

帮大家扩个句哦:

while(n--)
{
	if(s!=f)
	{
		if(s>f)
			cout<<(t+m-(m+2-s))/m*m + (m+2-f) -1 <<endl;
		else 
			cout<<(t+m-s)/m*m +f-1<<endl;
	}
	else cout<<t<<endl;
}

当然替换前需要把m=m*2-2写在前头哦。


为了便于理解,再放一个别人的写的比较分开的代码,可能会更加清晰明朗一些吧:

Code By MK3@Codeforces

#include <iostream>
using namespace std;

int main()
{ 
  int n,m;
  int p,s,f,t,x; 
  cin >> n >> m; p=(m-1)*2; 
  while(n--)
  {
    cin >> s >> f >> t; 
    if(s==f) cout << t << endl; 
    else
    {
      x = t / p*p + (s<f ? s-1 : 2*m-1-s); 
      cout << (x<t ? x+p : x) + (f>s ? f-s : s-f) << endl; 
    }
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值