Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main(){
long n(0), m(0); scanf("%ld %ld", &n, &m);
while(n--){
long s(0), f(0), t(0), output(0);
scanf("%ld %ld %ld", &s, &f, &t);
if(s == f){output = t;}
else{
long T = 2 * (m - 1); //Period of a round trip
--s; --f; //For modulo arithmetic to work
output = (t/T) * T;
if(s < f && (output + s) < t){output += T;}
else if(s > f && (output + T - s) < t){output += T;}
output += (s < f) ? f : (T - f);
}
printf("%ld\n", output);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
7 4
2 4 3
1 2 0
2 2 0
1 2 1
4 3 5
1 2 2
4 2 0
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
1
0
7
10
7
5
Demonstration
Codeforces Solution-A. Elevator-Solution in C, C++, Java, Python