Advertisement
Guest User

kek

a guest
Oct 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = 1e9;
  6.  
  7. int prev(int a, int b) {
  8.     return a < b ? a + 1 : a - 1;
  9. }
  10.  
  11. int *cpos, *clifetime;
  12.  
  13. int dist(int a, int b) {
  14.     return abs(cpos[a] - cpos[b]);
  15. }
  16.  
  17. int cash[1 << 10][1 << 10];
  18. void clear_cash() {
  19.     for (int i = 0; i < (1 << 10); ++i) {
  20.         for (int j = 0; j < (1 << 10); ++j) {
  21.             cash[i][j] = (i != j) * (-1);
  22.         }
  23.     }
  24. }
  25.  
  26. int bust(int a, int b) {
  27.     if (cash[a][b] != -1) {
  28.         return cash[a][b];
  29.     }
  30.     int x = bust(prev(a, b), b), dx = dist(prev(a, b), a),
  31.         y = bust(b, prev(a, b)), dy = dist(b, a);
  32.     cash[a][b] = INF;
  33.     if (x != INF && x + dx <= clifetime[a]) {
  34.         cash[a][b] = min(cash[a][b], x + dx);
  35.     }
  36.     if (y != INF && y + dy <= clifetime[a]) {
  37.         cash[a][b] = min(cash[a][b], y + dy);
  38.     }
  39.     return cash[a][b];
  40. }
  41.  
  42. int main(){
  43.  
  44.     clear_cash();
  45.  
  46.     int n;
  47.     cin >> n;
  48.  
  49.     cpos = new int[n], clifetime = new int[n];
  50.     for (int i = 0; i < n; ++i) {
  51.         cin >> cpos[i] >> clifetime[i];
  52.     }
  53.  
  54.     int res = min(bust(0, n - 1), bust(n - 1, 0));
  55.     if (res == INF) {
  56.         cout << "No solution";
  57.     } else {
  58.         cout << res;
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement