Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int INF = 1e9;
- int prev(int a, int b) {
- return a < b ? a + 1 : a - 1;
- }
- int *cpos, *clifetime;
- int dist(int a, int b) {
- return abs(cpos[a] - cpos[b]);
- }
- int cash[1 << 10][1 << 10];
- void clear_cash() {
- for (int i = 0; i < (1 << 10); ++i) {
- for (int j = 0; j < (1 << 10); ++j) {
- cash[i][j] = (i != j) * (-1);
- }
- }
- }
- int bust(int a, int b) {
- if (cash[a][b] != -1) {
- return cash[a][b];
- }
- int x = bust(prev(a, b), b), dx = dist(prev(a, b), a),
- y = bust(b, prev(a, b)), dy = dist(b, a);
- cash[a][b] = INF;
- if (x != INF && x + dx <= clifetime[a]) {
- cash[a][b] = min(cash[a][b], x + dx);
- }
- if (y != INF && y + dy <= clifetime[a]) {
- cash[a][b] = min(cash[a][b], y + dy);
- }
- return cash[a][b];
- }
- int main(){
- clear_cash();
- int n;
- cin >> n;
- cpos = new int[n], clifetime = new int[n];
- for (int i = 0; i < n; ++i) {
- cin >> cpos[i] >> clifetime[i];
- }
- int res = min(bust(0, n - 1), bust(n - 1, 0));
- if (res == INF) {
- cout << "No solution";
- } else {
- cout << res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement