Advertisement
Dang_Quan_10_Tin

CAU2 HSGHN L9 2014-2015

Dec 24th, 2021
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #define task "CAU2"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. constexpr int N = 1e3 + 5;
  10. constexpr int Inf = 1e9 + 7;
  11. int n, a[N], h, f[N];
  12.  
  13. void Read()
  14. {
  15.     cin >> n >> h;
  16.  
  17.     for (int i = 1; i <= n; ++i)
  18.         cin >> a[i];
  19. }
  20.  
  21. void Solve()
  22. {
  23.     sort(a + 1, a + n + 1);
  24.  
  25.     for (int i = 1; i <= n; ++i)
  26.     {
  27.         f[i] = Inf;
  28.         for (int j = i; j >= 1 && a[i] - a[j] <= h; --j)
  29.             f[i] = min(f[i], f[j - 1] + 1);
  30.     }
  31.  
  32.     cout << f[n];
  33. }
  34.  
  35. int32_t main()
  36. {
  37.     ios::sync_with_stdio(0);
  38.     cin.tie(0);
  39.     cout.tie(0);
  40.     if (fopen(task ".INP", "r"))
  41.     {
  42.         freopen(task ".INP", "r", stdin);
  43.         freopen(task ".OUT", "w", stdout);
  44.     }
  45.  
  46.     Read();
  47.     Solve();
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement