it's my second time asking questions here. Please let me know if I need more information/obey some kind of rules when posting. Now let's hop straight into this problem.
Here's the link the the problem. http://ift.tt/1BsPULD
I have worked on it for an hour yesterday and half an hour today, tried multiple solutions but couldn't figure out when Codechef is giving me "wrong answer". Note that my program produced the same output as the example output in Codechef, and I even made up some of my own numbers and plug them in, and my program still gives the correct (I calculated them by myself) result.
My thought process is: ---- for starting time. ____ for traveling time. ++++ for waiting time.
------_____++++++
-----------------------____+++|+++|+++
In this case, the time required for traveling from station1 to station2 is starting time+traveling time for station2.
-----------------------____+++|+++|+++
-------______________++++|++++|++++|++++|
In this case, the time required for traveling from station2 to station3 is starting time, treveling time and 2 waiting time of station3.
And the total time would be the same from station2 to station3. Because it's linear?
Here's my code:
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
int n = 0;
int [] t = new int [cases];
for(int i = 0; i < cases; i++) {
int stations = in.nextInt();
int [] start = new int [stations];
int [] travel = new int [stations];
int [] gap = new int [stations];
//System.out.printtraveln(stations);
for(int j = 0; j < stations; j++) {
start[j] = in.nextInt();
travel[j] = in.nextInt();
gap[j] = in.nextInt();
//System.out.println(x[j]);
//System.out.println(l[j]);
//System.out.println(f[j]);
}
for(int j = 0; j < (stations-1); j++) {
//t[i] = start[j] + travel[j];
n = 0;
if(j==0) {
if(start[j] + travel[j] > start[j+1]+(n*gap[j+1])) {
while(start[j] + travel[j] > start[j+1]+(n*gap[j+1])) {
n++;
}
t[i] = start[j+1] + (n*gap[j+1]) + travel[j+1];
} else {
t[i] = start[j+1] + travel[j+1];
}
} else {
if(t[i] > start[j+1]+(n*gap[j+1])) {
while(t[i] > start[j+1]+(n*gap[j+1])) {
n++;
}
t[i] = start[j+1] + (n*gap[j+1]) + travel[j+1];
} else {
t[i] = start[j+1] + travel[j+1];
}
}
}
}
for(int i = 0; i < cases; i++) {
System.out.println(t[i]);
}
}
}
Thanks in advance for your help! Any comment/suggestions/discussions are appreciated! :)
Also, feel free to tag people who are interested in this kind of stuff!
Aucun commentaire:
Enregistrer un commentaire