HDOJ 3732

Ahui Writes Word

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1746    Accepted Submission(s): 654


Problem Description
We all know that English is very important, so Ahui strive for this in order to learn more English words. To know that word has its value and complexity of writing (the length of each word does not exceed 10 by only lowercase letters), Ahui wrote the complexity of the total is less than or equal to C.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
 

Input
The first line of each test case are two integer N , C, representing the number of Ahui’s words and the total complexity of written words. (1 ≤ N ≤ 100000, 1 ≤ C ≤ 10000)
Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
 

Output
Output the maximum value in a single line for each test case.
 

Sample Input
   
   
   
   
5 20 go 5 8 think 3 7 big 7 4 read 2 6 write 3 5
 

Sample Output
   
   
   
   
15
Hint
Input data is huge,please use “scanf(“%s”,s)”
 

Author
Ahui
 

Source
ACMDIY第二届群赛
 

Recommend
notonlysuccess
 
01背包TLE,后来才得知可以多重背包。
物品个数很大,背包的体积很大,但是物品的价值和花费都很小。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define mid ((l + r) >> 1)
#define mk make_pair
const int MAXN = 100000 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 10007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
int dp[MAXN];
int n , m , a , v;
int cnt[11][11];///用于统计相同价值和费用的单词的数目

char str[30];
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    while(~scanf("%d%d" , &n , &m))
    {
        clr(cnt , 0);
        FOR(i , 0 , n)
        {
            scanf("%s%d%d" , str ,& a , &v);
            cnt[a][v]++;
        }
        clr(dp , 0);
        FOR(i , 0 , 11)FOR(j , 0 , 11)
        {
            if(cnt[i][j] == 0)continue;
            if(cnt[i][j] * j >= m)///完全背包 
            {
                FORR(k , j , m)dp[k] = max(dp[k] , dp[k - j] + i);
            }
            else///01背包
            {
                int l = 1;
                while(l < cnt[i][j])
                {
                    REPP(k , m , l * j)
                    {
                        dp[k] = max(dp[k] ,  dp[k - l * j] + l * i);
                    }
                    cnt[i][j] -= l;
                    l <<= 1;
                }
                REPP(k , m , cnt[i][j] * j)dp[k] = max(dp[k] , dp[k - cnt[i][j] * j] +  cnt[i][j] * i);
            }
        }
        printf("%d\n" , dp[m]);
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)