am1x

p102178c

Sep 11th, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #define __STDC_FORMAT_MACROS
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <inttypes.h>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8.  
  9. typedef unsigned int uint_t;
  10. const uint_t N = 50;
  11.  
  12. static struct Solver {
  13.     uint_t best;
  14.     uint64_t a;
  15.     std::vector<uint_t> ts;
  16.     std::vector<uint64_t> ms;
  17.  
  18.     void init(uint_t mx, uint64_t _a) {
  19.         best = 6;
  20.         a = _a;
  21.         ts.clear();
  22.         ts.push_back(mx);
  23.         ms.clear();
  24.         ms.push_back(1);
  25.     }
  26.    
  27.     void solve()
  28.     {
  29.         uint_t n = ts.size() - 1;
  30.         uint64_t mn = ms[n];
  31.         if ((a & ~mn) == 0) {
  32.             if (n < best)
  33.                 best = n;
  34.             return;
  35.         }
  36.        
  37.         if (n + 1 >= best)
  38.             return;
  39.        
  40.         for (uint_t x = ts[n]; x; x--) {
  41.             ts.push_back(x);
  42.             ms.push_back(mn | (mn << x));
  43.             solve();
  44.             ts.pop_back();
  45.             ms.pop_back();
  46.         }
  47.     }
  48. } s;
  49.  
  50.  
  51.  
  52. int main()
  53. {
  54.     uint64_t a = 0;
  55.     uint_t n = 0, mx = 0;
  56.     int st = scanf("%u", &n);
  57.     assert (st == 1);
  58.     assert (0 < n && n <= N);
  59.    
  60.     for (uint_t i = 0; i < n; i++) {
  61.         uint_t x = 0;
  62.         st = scanf("%u", &x);
  63.         assert (st == 1);
  64.         assert (0 < x && x <= N);
  65.         a |= 1ULL << x;
  66.         if (mx < x)
  67.             mx = x;
  68.     }
  69.  
  70.     s.init(mx, a);
  71.     s.solve();
  72.     printf("%u", s.best);
  73.  
  74.     return 0;
  75. }
  76.  
Add Comment
Please, Sign In to add comment