基础-DP

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … 
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ? 

Input

The first line contain a integer T , the number of cases. 
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 2 31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14
#include<iostream>
using namespace std;
#include<math.h>
#include<string.h>
#define maxn 100000
long long d[maxn+5];
int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
int main()
{
    int t,n,m,i,j;
    int V[1005],N[1005];
    cin>>t;
    while(t--)
    {
        memset(d,0,sizeof(d));
        cin>>n>>m;
        for(i=0;i<n;i++)
            cin>>V[i];
        for(j=0;j<n;j++)
            cin>>N[j];
        for(i=0;i<n;i++)
            for(j=m;j>=N[i];j--)
        {
            d[j]=max(d[j],d[j-N[i]]+V[i]);
        }
        cout<<d[m]<<endl;
    }
    return 0;
}  
时间: 2024-10-13 22:23:19

基础-DP的相关文章

基础dp

队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加深了对dp的理解,但要想搞好dp,还需要多多练习啊. HDU - 1024 开场高能 给出一个数列,分成m段,求这m段的和的最大值,dp[i][j]表示遍历到第i个数,已经划分了j段,对于每一个数有两种决策,加入上一个区间段,自己成为一个区间段,故dp[i][j] = max(dp[i-1][j]+

POJ 3616 Milking Time 基础DP

Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5743   Accepted: 2401 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤

hdu 5586 Sum 基础dp

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,all the numbers Ai(l≤i≤r) will become f(Ai).f(x)=(1890x+143)mod1

基础DP 19道

VJ链接:点击打开链接 基础DP做好了更有益~! 从中得出几个结论: 1. 背包问题所选的物品是没有相关性,是填充性质 2. LIS问题是元素之间有某种关系(多个属性则先排序某个,在依据另一个LIS) 3. TSP组合问题,一般进行状压,求元素的某种序 题目: 1. 最大M子段和 这个很像多维背包问题,有个数限制.同时我们可以发现最后这个元素只能是  i个子段中最后一个子段 dp[i][j]用来表示由前 j项得到的含i个字段的最大值,且最后一个字段以num[j]项结尾. dp[i][j]=max

FatMouse&#39;s Speed 基础DP

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13643    Accepted Submission(s): 6011Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster i

基础DP总结

---恢复内容开始--- 关键词:基础DP问题,LIS,LCS,状压DP 简析 :DP大法好啊,当一个大问题不好解决的时候,我们研究它与其子问题的联系,然后子问题又找它的子问题,如此下去,一直推,一直减小到可以轻而易举求出答案(称为边界).所以解决DP问题就是要推出一个正确的递推式. 一.DP解决基础递推问题 1)斐波那契数列 dp[n] = dp[n-1] + dp[n-2] 边界:dp[0] = 0 , dp[1] = 1 . 2 )Max sum 题目链接 : http://acm.hdu

FatMouse&#39;s Speed ~(基础DP)打印路径的上升子序列

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speed

「kuangbin带你飞」专题十二 基础DP

layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathjax: true - kuangbin - 动态规划 传送门 A.HDU1024 Max Sum Plus Plus 题意 给你N个数,然后你分成M个不重叠部分,并且这M个不重叠部分的和最大. 思路 动态规划最大m字段和,dp数组,dp[i][j]表示以a[j]结尾的,i个字段的最大和 两种情况:1.第a[j

训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - 基础DP - Dijkstra - 图论 - 训练指南 Walk Through the Forest UVA - 10917 题意 Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比

Apple Catching POJ 2385(基础dp)

原题 题目链接 题目分析 基础dp题,按照题意很容易给出dp定义,dp[i][j],表示在i时间内,用j次转移机会得到的最大苹果数.dp转移如下,如果j==0,则dp[i][j]=dp[i-1][j],否则 如果当前位置有苹果dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+1.否则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]).最后在dp[T][j]里找最大值就行了,(0<=j<=W). 代码 1 #include <iostrea