Advertisement
royalsflush

Referência para TopCoder CollectingPostmarks (Luiza)

Apr 6th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <math.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <queue>
  11. using namespace std;
  12.  
  13. #define TRACE(x) x
  14. #define PRINT(x) TRACE(printf(x))
  15. #define WATCH(x) TRACE(cout << #x << " = " << x << endl;)
  16.  
  17. #define rep(i,n) for (int i=0; i<n; i++)
  18.  
  19. const int inf =0x3f3f3f3f;
  20. const double eps = 1e-9;
  21.  
  22. pair<int,int> p1[(1<<16)+10];
  23. pair<int,int> p2[(1<<16)+10];
  24. int szP1, szP2;
  25. vector<int> _prices;
  26. vector<int> _values;
  27. int _K;
  28.  
  29. int compute(int beg, int end, pair<int,int>* p) {
  30.     int sz = end-beg;
  31.     int cnt=0;
  32.  
  33.     for (int i=0; i<(1<<sz); i++) {
  34.         int v=0,pr=0;
  35.  
  36.         for (int j=0; j<sz; j++)
  37.             if (i&(1<<j)) {
  38.                 int idx=j+beg;
  39.                 pr+=_prices[idx];
  40.                 v+=_values[idx];
  41.             }
  42.  
  43.         p[cnt++]=make_pair(v,pr);
  44.     }
  45.  
  46.     sort(p,p+cnt);
  47.    
  48.     for (int i=0; i<cnt; i++)
  49.         printf("%d, %d\n", p1[i].first, p1[i].second);
  50.  
  51.     return cnt;
  52. }
  53.  
  54. int combine() {
  55.     long long minMoney=INT_MAX;
  56.     long long last=INT_MAX;
  57.  
  58.     int it2=szP2-1;
  59.     for (int it1=0; it1<szP1; it1++) {
  60.         while (it2>=0 && p1[it1].first+p2[it2].first>=_K)
  61.             last = min(last, (long long)p2[it2--].second);
  62.         minMoney=min(minMoney,last+p1[it1].second);
  63.     }
  64.  
  65.     return (int)minMoney;
  66. }
  67.  
  68. class CollectingPostmarks {
  69. public:
  70.    int amountOfMoney( vector <int> prices, vector <int> have, vector <int> values, int K ) {
  71.     int sumValues=0;
  72.     int n = prices.size();
  73.     _K=K;
  74.     _prices=prices;
  75.     _values=values;
  76.  
  77.     for (int i=0; i<n; i++)
  78.         sumValues+=values[i];
  79.     if (sumValues<K) return -1;
  80.  
  81.     szP1 = compute(0,n/2,p1);
  82.     szP2 = compute(n/2,n,p2);
  83.  
  84.     int need=combine();
  85.     int moneyHave=0;
  86.  
  87.     for (int i=0; i<have.size(); i++)
  88.         moneyHave+=prices[have[i]];
  89.  
  90.     return max(need-moneyHave,0);
  91.    }
  92. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement