Little_hobbit

500 Агент - динамика

Jul 10th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. struct agent
  7. {
  8.     int age;
  9.     int danger;
  10. };
  11.  
  12. int compare(agent a, agent b)
  13. { return a.age < b.age; }
  14.  
  15. int main( )
  16. {
  17.     FILE *in = fopen("input.txt", "rt"),
  18.             *out = fopen("output.txt", "wt");
  19.  
  20.  
  21.     int n;
  22.     fscanf(in, "%d", &n);
  23.  
  24.     agent potok[n];
  25.     for (int i = 0; i < n; ++i)
  26.     {
  27.         fscanf(in, "%d %d", &potok[i].age, &potok[i].danger);
  28.     }
  29.  
  30.     sort(potok, potok + n, compare);
  31.     int d[n];
  32.     d[1] = potok[1].danger;
  33.     d[2] = d[1] + potok[2].danger;
  34.     for (int i = 3; i < n; ++i)
  35.     {
  36.         d[i] = min(d[i-1], d[i-2]) + potok[i].danger;
  37.     }
  38.  
  39.     fprintf(out, "%d", d[n - 1]);
  40.     fclose(in);
  41.     fclose(out);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment