damihenrique

Untitled

Apr 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <cstring>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <utility>
  8. #include <queue>
  9. #include <map>
  10. #include <stack>
  11. #include <cmath>
  12. #include <set>
  13. #include <ctype.h>
  14. #include <bitset>
  15.  
  16. #define INF 0x3F3F3F3F
  17. #define rep(i, a, b) for (int i = int(a); i < int(b); i++)
  18. #define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
  19. #define tr(it, s)  for ( typeof ( s.begin( ) ) it=s.begin( ); it!=s.end( ); it++ )
  20. #define pb push_back
  21. #define clr(a) memset((a),0,sizeof(a))
  22. #define pi 3.1415926535897932384626433832795028841971
  23. #define debug(x) cout << #x << " = " << x << endl;
  24. #define debug2(x,y) cout << #x << " = " << x << " --- " << #y << " " << y << "\n";
  25. #define all(S) (S).begin(), (S).end()
  26. #define MAXV 1005
  27. #define F first
  28. #define S second
  29. #define EPS 1e-9
  30.  
  31. using namespace std;
  32.  
  33. typedef long long ll;
  34. typedef pair < int, int >  ii;
  35. typedef vector < int >  vi;
  36. typedef vector < ii >  vii;
  37.  
  38. int pd[102][55], n;
  39. int memo[102][55];
  40. int sum, qq;
  41. map<int, int> mp;
  42.  
  43. struct pre{
  44.     int peso, qtdade;    
  45. };
  46.  
  47. pre p[102];
  48.  
  49. bool comp(pre a, pre b){
  50.     if(a.peso < b.peso) return 1;
  51.     else if(a.peso > b.peso) return 0;
  52.     else if(a.qtdade > b.qtdade) return 1;
  53.     else return 0;
  54. }
  55.  
  56. int solve(int i, int c){
  57.     if(i == n) return 0;
  58.     if(pd[i][c] != -1) return pd[i][c];  
  59.     int a = solve(i+1, c), b=0;
  60.     if(c+p[i].peso <= 50)
  61.         b = solve(i+1, c+p[i].peso) + p[i].qtdade;
  62.     if(b > a)  memo[i][c] = 1;
  63.     return pd[i][c] = max(a, b);
  64. }
  65.  
  66.  
  67. void back(int i, int c){
  68.     if(i == n) return;
  69.     if(memo[i][c]){
  70.        sum += p[i].peso;
  71.        qq++;
  72.        back(i+1, c+p[i].peso);
  73.     }
  74.     else back(i+1, c);
  75. }
  76.  
  77. int main(){
  78.  
  79.     int tc, qt, kg;
  80.      
  81.     cin>>tc;
  82.      
  83.     while(tc--){
  84.         sum = qq = 0;
  85.         cin>>n;
  86.         rep(i,0,n)
  87.             scanf("%d%d",&p[i].qtdade, &p[i].peso);  
  88.            
  89.         sort(p,p+n,comp);      
  90.          
  91.         rep(i,0,n+1)
  92.         rep(j,0,51){
  93.             pd[i][j] = -1;
  94.             memo[i][j] = 0;
  95.         }
  96.          
  97.         int ans = solve(0,0);
  98.         cout<<ans<<" brinquedos\n";      
  99.         back(0,0);
  100.         cout<<"Peso: "<<sum<<" kg\n";
  101.         cout<<"sobra(m) "<<n - qq<<" pacote(s)\n\n";  
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment