Sunday, January 12, 2014

codechef COMMUTE - "the morning commute" solution

codechef COMMUTE - "the morning commute": http://www.codechef.com/problems/COMMUTE

editorial: http://discuss.codechef.com/problems/COMMUTE

codechef COMMUTE - "the morning commute" solution: http://ideone.com/pHohIL

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
int t, n, x, l, f, a;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%d %d %d", &x, &l, &f);
if(i==0) a=x+l;
else {
if(a<=x) {
a=x; a += l;
}
else {
while(a>x) x += f;
a=x; a += l;
}
}
}
printf("%d\n", a);
}
return 0;
}

guidance: on 1st station(i=0), just add x+l, starting time and time between 1st and 2nd station. however, on following stations, if chef arrived earlier than trains departure time(a<x), then chef waits till train depart(a=x). if chef has missed the train(a>x), then wait till next train depart(x=x+f). then update time(a=x; a=a+l;). follow same approach till last station. that is all.

No comments:

Post a Comment