3991. Eat or Study
Time Limit: 1.0 Seconds Memory Limit:65536K
Total Runs: 127 Accepted Runs:43
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
32 33 23 33 103 23 33 33 52 102 1002 50
Sample Output
3JUMP DOWN!0
dp[i]表示能滿足i點知識點的情況下取得最小的重量
大俠的代碼
#include<cstdio>#include<iostream>using namespace std;#define INF 100001int main(){int t;int x[150],y[150];int dp[150];cin>>t;while(t--){int n,m;int ans=0;cin>>n>>m;for(int i=0;i<=m;i++)dp[i]=INF;for(int i=0;i<n;i++){cin>>x[i]>>y[i];ans+=y[i];}for(int i=0;i<n;i++)for(int j=m;j>=0;j--)if(j-x[i]<=0)dp[j]=min(dp[j],y[i]);elsedp[j]=min(dp[j],dp[j-x[i]]+y[i]);if(dp[m]<INF)cout<<(ans-dp[m])<<endl;elsecout<<"JUMP DOWN!"<<endl;}return 0;}
錯誤碼 還沒找出哪裡錯
#include<cstdio>#include<iostream>using namespace std;#define INF 100001int main(){int t;int x[150],y[150];int dp[15000];cin>>t;while(t--){int n,m;int ans=0;int sum=0;cin>>n>>m;for(int i=0;i<n;i++){cin>>x[i]>>y[i];ans+=y[i];sum+=x[i];}for(int i=0;i<=sum;i++)dp[i]=INF;dp[0]=0;for(int i=0;i<n;i++)for(int j=sum;j>=x[i];j--)if(dp[j-x[i]]<INF)dp[j]=min(dp[j],dp[j-x[i]]+y[i]);int flag=1;for(int i=m;i<=sum;i++)if(dp[i]<=ans){flag=0;cout<<(ans-dp[i])<<endl;break;}if(flag)cout<<"JUMP DOWN!"<<endl;}return 0;}