#88 A
problem link http://codeforces.com/contest/117/problem/A
19:18 - 20:42
3 key point of this problem.
a) determin which is the current floor.
b) determin elevator direction
c) judge the time base on the s, cf, f relationship and direction. ( s--start floor, cf--current floor, f--final floor )
None of the above point can be omitted.
#include <stdio.h>
int main() {
int n, m;
scanf("%d %d", &n, &m);
int i, s, f, t, cf, d;
int wait, go, time;
for( i=0;i<n;i++ ) {
scanf("%d %d %d", &s, &f, &t);
if( s == f ) {
printf("%d\n", t);
continue;
}
d = (t/(m-1)) % 2;
if( d == 0 )
cf = (t % (m-1)) + 1;
else
cf = m - (t % (m-1));
if( cf < s && s < f ) {
if( d == 0 ) // up
time = f - cf;
else // down
time = cf - 1 + f - 1;
}
else if( s < cf && cf < f ) {
if ( d == 0 ) // up
time = m - cf + m - 1 + f - 1;
else // down
time = cf - 1 + f - 1;
}
else if( s < f && f < cf ) {
if ( d == 0 ) // up
time = m - cf + m - 1 + f - 1;
else // down
time = cf - 1 + f - 1;
}
else if( cf < f && f < s ) {
if ( d == 0 ) // up
time = m - cf + m - f;
else // down
time = cf - 1 + m - 1 + m - f;
}
else if( f < cf && cf < s ) {
if ( d == 0 ) // up
time = m - cf + m - f;
else // down
time = cf - 1 + m - 1 + m - f;
}
else if( f < s && s < cf ) {
if ( d == 0 ) // up
time = m - cf + m - f;
else // down
time = cf - f;
}
else if( s == cf && cf < f ) {
if( d == 0 ) // up
time = f - cf;
else // down
time = cf - 1 + f - 1;
}
else if( s < cf && cf == f ) {
if( d == 0 ) // up
time = m - cf + m - 1 + f - 1;
else // down
time = cf - 1 + f - 1;
}
else if( f == cf && cf < s ) {
if( d == 0 ) // up
time = m - cf + m - f;
else // down
time = cf - 1 + m - 1 + m - f;
}
else if( f < cf && cf == s ) {
if( d == 0 ) // up
time = m - cf + m - f;
else // down
time = cf - f;
}
printf("%d\n", time + t);
}
return 0;
}