I have been trying to solve this problem, but I don’t know how should I approach this problem. I think this is a graph problem, then again I am not sure what algorithm should I use here. Can someone give me a hint or point me in the right direction which algorithm should I use?
Problem Statement
One day in the year 3019, a high school student named Moreno is going to school as usual. The town he lives in can be represented as a straight line stretching from east to west. Moreno’s house is at coordinate 0, and his school is at S. There are N stores and M teleport stations between the house and the school. Counting from the westernmost point, the i th (1 ≤ i ≤ N) store is at coordinate a_{i}, and the j th (1 ≤ j ≤ M) teleport station is at b_{j}. He can move between any two teleport stations in zero seconds.
When he walks, it takes one second to walk one coordinate.
Moreno is leaving home at time 0, measured in seconds.
(Note that the unit of time is always ‘seconds’ in this problem.)
He has to get to school no later than the time T.
If he gets to school at time T + 1 or later, it will be treated as being late for school, and he will be kicked out of school.
However, he really likes taking side trips, so he wants to shop at as many stores as possible on the way to school.
He can only shop at a store if he is at the same coordinate as the store.
It takes one second to shop at each store.
Calculate the maximum number of stores P that Moreno can shop at without being late for school.
Also calculate the minimum time Q it will take for Moreno to get to school when he shops at P stores along the way.
Input Rules
The format of the standard input is as below:
N M S T
a_{1} a_{2} … a_{N}
b_{1} b_{2} … b_{M}
Restrictions:
- 1 ≤ N ≤ 2000, Integer
- 2 ≤ M ≤ 2000, Integer
- 1 ≤ S ≤ 10^{9}, Integer
- 1 ≤ T ≤ 10^{9}, Integer
- 1 ≤ a_{1} < a_{2} < a_{3} < … < a_{N} < S, Integer
- 1 ≤ b_{1} < b_{2} < b_{3} < … < b_{M} < S, Integer
- b_{1} < a_{1} and a_{N} < b_{M}
- The maximum number of buildings (houses, schools, stores, teleport stations) on the same coordinate is 1.
- No inputs in which Moreno will be late for school no matter which way he moves are given.
Output Rules
The format of the standard output is as below:
P Q
- P: The maximum number of stores Moreno can shop at without being late for school.
- Q: The minimum time for Moreno to get to school when he shops at P stores along the way.
Input & Output samples
Sample 1
Sample Input 1
3 3 20 16
3 6 11
2 7 15
Sample Output 1
2 13
The best series of movements is as follows.
- Walk to the first store, shop there, then walk back to the first teleport station. (time: 5)
- Teleport to the second teleport station. (time: 5)
- Walk to the second store, shop there, then walk back to the second teleport station. (time: 8)
- Teleport to the third teleport station. (time: 8)
- Walk to the school. (time: 13)
If he shops at the third store, neither of the other two stores can be visited.
Sample 2
Sample Input 2
2 2 10 14
4 6
2 8
Sample Output 2
2 12
It is optimal for him to walk straight to the school, shopping at all the stores as he passes by.
Sample 3
Sample Input 3
7 2 10 2
2 3 4 5 6 7 8
1 9
Sample Output 3
0 2
There are many stores, but unfortunately he has no time to shop.