qduoj ycb's ACM advanced road (looks like 01's multiple backpacks)

Description

  ycb is a gifted and intelligent kid whose dream is to become the greatest ACMer in the world. To this end, he wanted to worship the most prestigious dalao nearby as his teacher. In order to judge his aptitude, dalao gave him a difficult problem. Dalao took him to an oj full of questions and said to him: "child, there are some different questions in this oj, it takes some time to do each question, and each question also has its own rp (personal value) ). I will give you a period of time during which you can do some questions. If you are a smart kid, you should be able to maximize the total rp of the questions." If you are ycb, you can do this task?

Input

The first line of the input file is a T, indicating the number of test groups, and the first line of each group of the next T groups contains two positive integers N, M. M represents the total time that can be used to do the questions, and N represents the number of questions in oj. Each of the next N lines includes two integers, which represent the time Ti for each question and the character value Vi for the question, respectively. 1 <= N, M <= 100000, 1 <= Ti, Vi <= 10

Output

The output file contains only an integer representing the maximum character value that can be obtained by doing the question within the specified time.

Sample Input 1 

1
3 9
10 10
8 1
1 2

Sample Output 1

3

Idea: Ti and Vi are very small, so we can open a two-dimensional array to record each [Ti, Vi], and then run a multi-knapsack binary optimization. Binary optimization is to choose 1, 2, 4, 8 each time. ..until it can't be taken, the remaining ones are also included, so that all possible values ​​can be represented by these, and then run a 01 backpack for each method.

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double esp = 1e-12;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n,m;
int mp[12][12];
int dp[maxn];

void init()
{
	mem(mp,0);
	mem(dp,0);
}

intmain()
{
	int t;
	cin>>t;
	while(t--)
	{
		init();
		scanf("%d %d",&n,&m);
		
		for(int i = 1;i<= n;i++)
		{
			int a,b;
			scanf("%d %d",&a,&b);
			mp [a] [b] ++;
		}
		
		for(int i = 1;i<= 10;i++)
		{
			for(int j = 1;j<= 10;j++)
			{
				int sum = mp[i][j],k = 1;
				while(sum> k)
				{
					for(int l = m;l>= k*i;l--)
						dp[l] = max(dp[l],dp[l-k*i]+k*j);
					
					sum = k;
					k*= 2;
				}
				
				if(!sum) continue;
				for(int l = m;l>= sum*i;l--)
					dp[l] = max(dp[l],dp[l-sum*i]+sum*j);
			}
		}
		
		printf("%d\n",dp[m]);
	}	
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325964356&siteId=291194637