Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <stdio.h>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- #include <utility>
- #include <queue>
- #include <map>
- #include <stack>
- #include <cmath>
- #include <set>
- #include <ctype.h>
- #include <bitset>
- #define INF 0x3F3F3F3F
- #define rep(i, a, b) for (int i = int(a); i < int(b); i++)
- #define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
- #define tr(it, s) for ( typeof ( s.begin( ) ) it=s.begin( ); it!=s.end( ); it++ )
- #define pb push_back
- #define clr(a) memset((a),0,sizeof(a))
- #define pi 3.1415926535897932384626433832795028841971
- #define debug(x) cout << #x << " = " << x << endl;
- #define debug2(x,y) cout << #x << " = " << x << " --- " << #y << " " << y << "\n";
- #define all(S) (S).begin(), (S).end()
- #define MAXV 1005
- #define F first
- #define S second
- #define EPS 1e-9
- using namespace std;
- typedef long long ll;
- typedef pair < int, int > ii;
- typedef vector < int > vi;
- typedef vector < ii > vii;
- int pd[102][55], n;
- int memo[102][55];
- int sum, qq;
- map<int, int> mp;
- struct pre{
- int peso, qtdade;
- };
- pre p[102];
- bool comp(pre a, pre b){
- if(a.peso < b.peso) return 1;
- else if(a.peso > b.peso) return 0;
- else if(a.qtdade > b.qtdade) return 1;
- else return 0;
- }
- int solve(int i, int c){
- if(i == n) return 0;
- if(pd[i][c] != -1) return pd[i][c];
- int a = solve(i+1, c), b=0;
- if(c+p[i].peso <= 50)
- b = solve(i+1, c+p[i].peso) + p[i].qtdade;
- if(b > a) memo[i][c] = 1;
- return pd[i][c] = max(a, b);
- }
- void back(int i, int c){
- if(i == n) return;
- if(memo[i][c]){
- sum += p[i].peso;
- qq++;
- back(i+1, c+p[i].peso);
- }
- else back(i+1, c);
- }
- int main(){
- int tc, qt, kg;
- cin>>tc;
- while(tc--){
- sum = qq = 0;
- cin>>n;
- rep(i,0,n)
- scanf("%d%d",&p[i].qtdade, &p[i].peso);
- sort(p,p+n,comp);
- rep(i,0,n+1)
- rep(j,0,51){
- pd[i][j] = -1;
- memo[i][j] = 0;
- }
- int ans = solve(0,0);
- cout<<ans<<" brinquedos\n";
- back(0,0);
- cout<<"Peso: "<<sum<<" kg\n";
- cout<<"sobra(m) "<<n - qq<<" pacote(s)\n\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment