mr_dot_convict

1047-Zones-UVa-mr.convict

Jun 6th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33. const int N = 20, M = 10;
  34. struct el {
  35.    int bit, val, len;
  36. } el[M];
  37.  
  38. int sz[N];
  39. int n, actual, m;
  40.  
  41. void read() {
  42.    for (int i = 0; i < n; ++i)
  43.       cin >> sz[i];
  44.  
  45.    cin >> m;
  46.    for (int i = 0; i < m; ++i) {
  47.       el[i] = {0, 0, 0};
  48.       int len; cin >> len;
  49.       el[i].len = len;
  50.       while (len--) {
  51.          int idx; cin >> idx;
  52.          --idx;
  53.          el[i].bit |= (1 << idx);
  54.       }
  55.       cin >> el[i].val;
  56.    }
  57. }
  58.  
  59. void solve(int tc) {
  60.    int mx_val = INT_MIN, best_sset = 0;
  61.  
  62.    for (int sub_set = 0; sub_set < (1 << n); ++sub_set) {
  63.       int val = 0;
  64.       if (__builtin_popcount(sub_set) == actual) {
  65.          for (int j = 0; j < n; ++j)
  66.             if (sub_set & (1 << j)) val += sz[j];
  67.  
  68.          for (int j = 0; j < m; ++j) {
  69.             if ((sub_set & el[j].bit)) {
  70.                int cnt = __builtin_popcount(sub_set & el[j].bit);
  71.                val -= el[j].val * (cnt - 1);
  72.             }
  73.          }
  74.          if (mx_val <= val) {
  75.             if (mx_val < val || (sub_set & (-sub_set)) < (best_sset & (-best_sset)))
  76.                best_sset = sub_set;
  77.             mx_val = val;
  78.          }
  79.       }
  80.    }
  81.  
  82.    cout << "Case Number  " << tc << '\n';
  83.    cout << "Number of Customers: " << mx_val << '\n';
  84.    cout << "Locations recommended: ";
  85.    for (int i = 0, f = 0; i < n; ++i) {
  86.       if (best_sset & (1 << i)) {
  87.          if (!f) ++f;
  88.          else cout << ' ';
  89.          cout << i + 1;
  90.       }
  91.    }
  92.    cout << "\n\n";
  93. }
  94.  
  95. signed main() {
  96.    IOS; PREC;
  97.  
  98.    int tc = 1;
  99.    while (cin >> n >> actual, n || actual) {
  100.       read();
  101.       solve(tc++);
  102.    }
  103.    return EXIT_SUCCESS;
  104. }
Add Comment
Please, Sign In to add comment