This is an easy problem from uva judge http://uva.onlinejudge.org/external/118/11850.html
How I did it
- I only use sorting stl and 1d array manipulation
- Save the information about station positions in an array
- Sort the array in non decreasing order
- Iterate through the array and check if all adjacents array positions are less or equal to 200
- Check the energy and distance for the last station. (The car must go to mille 1422 and return again). for this you can use this formula: 2*(1422 – vec[station-1])>200.
- Print the result
Hints
- Always the car began the travel with enough energy. In the position 0 has the first charge.
- Check the last station distance. Keep in mind that the car must travel to the mille 1422 and return again. Use the formula
#include <cstdio> #include <vector> #include <algorithm> using namespace std; int main(){ int station, position; bool flag; while(scanf("%d", &station)){ if(!station) break; vector<int> vec; for(int a=1; a<=station;a++) { scanf("%d", &position); vec.push_back(position); } sort(vec.begin(), vec.end()); flag = true; int a,b; for(a=0, b=1; b<vec.size(); a++,b++) { if(vec[b] - vec[a] > 200) { flag = false; break; } } if(flag == true) { if(2*(1422 - vec[station-1])>200) printf("IMPOSSIBLE\n"); else printf("POSSIBLE\n"); } else printf("IMPOSSIBLE\n"); } return 0; }