3991 H. Eat or Study

来源:互联网 发布:淘宝优惠券去哪里领 编辑:程序博客网 时间:2024/05/14 18:13

As a ACMer, Yan is good at arrange his schedule. On every morning of the term, he will have a choice: study or eating.
Especially, Yan can only do one thing everyday, and he have to do one thing everyday. That means Yan cannot both study and eat in one day, and he cannot do nothing on that day.If he choose study on the ith day, he will get xi knowledge points,and if he choose eating, his weight will increase yi points.After the term, Yan must face the final exam. If his knowledge points is less than m, he will fail in the exam. Now Yan wants to know the maximal value he can increase his weight without fail in the exam.
Input

The first line of input is a integer t (t≤100), means there are t test case.
For each test case, there are two integers in the first line, n(n≤100) and m(m≤100). n means there are n days in the term. m means that Yan have to get m knowledges when he face the final exam, or he will fail.Then n lines follow, each line contains two integer, xi(0≤xi≤100) and yi(0≤yi≤100). xi means he will get xi knowledge points if he study in the ith day, yi means he will get yi weight points if he eats in the ith day.

Output

For each test case, there is only one line.
If Yan can pass the exam, output the max weight he can increase without fail.
If Yan can’t pass the exam, output “JUMP DOWN!”
Sample Input

3
2 3
3 2
3 3
3 10
3 2
3 3
3 3
3 5
2 10
2 100
2 50
Sample Output

3
JUMP DOWN!
0

题意:给出你每天读书能增加的智慧和吃东西能增加的体重,问你如何在达到智慧要求的同时增最多的重量。

dp来做。

#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>using namespace std;int w[105],v[105],dp[105][105];int main (void){    int t;    cin>>t;    while(t--)    {        memset(dp,-1,sizeof(dp));        memset(w,0,sizeof(w));        memset(v,0,sizeof(v));        int n,m;        scanf("%d %d",&n,&m);        dp[0][0]=0;        for(int i=1;i<=n;i++)        {            scanf("%d %d",&v[i],&w[i]);        }        //dp[i][j]表示第i天学了j知识时候的体重        for(int i=1;i<=n;i++)        {            for(int j=0;j<=m;j++)            {                if(dp[i-1][j]!=-1)//判断前一个状态能否成立                {                    dp[i][j]=max(dp[i][j],dp[i-1][j]+w[i]);//今天不读书                    //昨天吃了的重量和昨天没吃,今天吃了的重量                    int nxt=j+v[i]>m?m:j+v[i];                    //今天读书,如果超过了m,就按照m来算                    dp[i][nxt]=max(dp[i-1][j],dp[i][nxt]);                    //昨天读了书的重量和昨天没读书,今天读了书的重量                }            }        }        if(dp[n][m]==-1)            printf("JUMP DOWN!\n");        else            printf("%d\n",dp[n][m]);    }    return 0;}
0 0
原创粉丝点击