hdu 5410 CRB and His Birthday(动态规划)

题目:

CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 748    Accepted Submission(s): 395

Problem Description

Today is CRB‘s birthday. His mom decided to buy many presents for her lovely son.

She went to the nearest shop with M
Won(currency unit).

At the shop, there are N
kinds of presents.

It costs Wi
Won to buy one present of i-th
kind. (So it costs k
× Wi
Won to buy k
of them.)

But as the counter of the shop is her friend, the counter will give
Ai × x + Bi
candies if she buys x(x>0)
presents of i-th
kind.

She wants to receive maximum candies. Your task is to help her.

1 ≤ T
≤ 20

1 ≤ M
≤ 2000

1 ≤ N
≤ 1000

0 ≤ Ai, Bi
≤ 2000

1 ≤ Wi
≤ 2000

Input

There are multiple test cases. The first line of input contains an integer
T,
indicating the number of test cases. For each test case:

The first line contains two integers M
and N.

Then N
lines follow, i-th
line contains three space separated integers Wi,
Ai
and Bi.

Output

For each test case, output the maximum candies she can gain.

Sample Input

1
100 2
10 2 1
20 1 1

Sample Output

21

Hint

CRB‘s mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.

 

Author

KUT(DPRK)

Source

2015 Multi-University Training Contest 10

Recommend

wange2014

题意:给了N种礼物,总共M块钱,每件礼物W【i】元,买x个第i种礼物可以得到A[I]*x+B[i]个糖果,问最多能得到多少糖果。

思路:看起来像完全背包,但有一些不同,完全背包是第i种物品的价值为P[i],也就是说买x个第i种物品能得到x*p[i]的价值,而这里是A[I]*x+B[i],所以要分是否是第一次买这个礼物,如果是第一次,那么增加的价值是A[i]+B[i],否则增加A[i].设dp[i][j][0]代表有j元钱,并且现在不买第i物品,dp[i][j][1]代表买这个物品,那么dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]),dp[i][j][1]=max(dp[i][j-w[i]][0]+A[i]+B[i],dp[i][j-w[i]][1]+A[i]).

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

int W[1005],A[1005],B[1005];
long long int dp[2005][2];

int main()
{int T;
RI(T);
while(T--)
{
    int M,N;
    RII(M,N);
    for(int i=1;i<=N;i++)
        RIII(W[i],A[i],B[i]);
    MS0(dp);
    for(int i=1;i<=N;i++)
        for(int m=0;m<=M;m++)
        {
           dp[m][0]=max(dp[m][0],dp[m][1]);
           if(m>=W[i])
           dp[m][1]=max(dp[m-W[i]][0]+A[i]+B[i],dp[m-W[i]][1]+A[i]);
           else
            dp[m][1]=0;

        }
        printf("%I64d\n",max(dp[M][0],dp[M][1]));

}

        return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-27 07:22:22

hdu 5410 CRB and His Birthday(动态规划)的相关文章

HDU 5410 CRB and His Birthday

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of presents

HDU 5410 CRB and His Birthday(DP)

CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 83    Accepted Submission(s): 45 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her l

hdu 5410 CRB and His Birthday(混合背包)

Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son. She went to the nearest shop with M Won(currency unit). At the shop, there are N kinds of presents. It costs Wi Won to buy one present of i-th kind.

hdu 5410 CRB and His Birthday 01背包和完全背包

#include<stdio.h> #include<string.h> #include<vector> #include<queue> #include<algorithm> using namespace std; int main() { int _,i,j,m,n,k,a[1024],b[1024],w[1024],dp[2048]; scanf("%d\n",&_); while(_--) { scanf(

HDU 5410 CRB and His Birthday ——(完全背包变形)

对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <set> 5 using namespace std; 6 typedef pair<int,int> pii; 7 8 pii dp[2005]; 9 int w[1005],A[1005

HDU 5410 CRB and His Birthday (01背包,完全背包,混合)

题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一次.然后对这种商品来一次完全背包,此时不送糖果,也可以多买. 1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 #define INF 0x7f7f7f7f 4 #define LL long long 5 using n

HDU 5410 CRB and His Birthday (2015年多校比赛第10场)

1.题目描述:点击打开链接 2.解题思路:本题是完全背包问题的一种变形.根据题意描述,每种物品的价值随着A[i]是线性变化的,但是并不随着B[i]线性变化,B[i]仅仅是在第一次挑选第i件物品是才算入,其他时候均不算入.因此,这里的状态要比普通的完全背包增加一个维度:是否是第一次选第i件物品,即用(i,j,flag)表示当前背包容量为j时,是否为第一次选第i件物品时的最大价值.那么不难得到如下状态转移方程: dp(i+1,j,0)=max{dp(i,j,0),dp(i,j,1)}; dp(i+1

HDU 5410(2015多校10)-CRB and His Birthday(完全背包)

题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话,会有Ai*x+Bi颗糖果,现给出每种礼物的单价.Ai值与Bi值,问最多能拿到多少颗糖果. 思路:完全背包问题. dp[j][1]在当前物品时花钱为j的并且买过当前物品的最大值. dp[j][0]不买当前这件物品此前花钱为j的的最大值. 每种物品的价值随Ai线性变化,但是不随B[i]线性变化,B[i]仅是在第一次挑选第i件物品是才算入,其他时候均不算入.所以我们可以写出状态转移方程: dp[j][0]=max(dp[j]

hdu 5412 CRB and Queries(线段树套笛卡尔树 - 动态区间第k大)

题目链接:hdu 5412 CRB and Queries 首先对所有出现过的值排序,建立线段树,每个线段树的节点是一棵笛卡尔树,笛卡尔树记录区间下标值. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; #define lson(x) (x<<1) #define rson(x) ((x<<