Little_hobbit

029 Компьютерная игра - Динмамика

Aug 23rd, 2020 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. // Ссылка на задачу https://acmp.ru/index.asp?main=task&id_task=29
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     unsigned n;
  10.     cin >> n;
  11.  
  12.     long long int arr[n], dist[n];
  13.     for (int i = 0; i < n; ++i)
  14.     {
  15.         cin >> arr[i];
  16.     }
  17.  
  18.     dist[0] = 0;
  19.     for (int i = 1; i < n; ++i)
  20.     {
  21.         if (i == 1)
  22.             dist[1] = abs(arr[1] - arr[0]);
  23.         else
  24.         {
  25.             long long int a = dist[i-1] + abs(arr[i] - arr[i-1]);
  26.             long long int b = dist[i-2] + 3*abs(arr[i] - arr[i-2]);
  27.             dist[i] = (a < b) ? a :  b;
  28.         }
  29.     }
  30.  
  31.     cout << dist[n-1] << endl;
  32.  
  33.     return 0;
  34. }
Add Comment
Please, Sign In to add comment