Picking Herbs (Super Simple DP)

Topic link: http://noi.openjudge.cn/ch0206/1775/

Description Chenchen is a child with great potential and talent, and his dream is to be called the greatest physician in the world. To this end, he wanted to learn from the most prestigious physicians nearby. In order to judge his qualifications, the doctor gave him a difficult problem. The doctor took him to a cave full of herbs and said to him: "Son, there are some different herbs in this cave. It takes some time to pick each one, and each one has its own value. I will give it to me. You can pick up some herbs for a period of time. If you are a smart child, you should be able to maximize the total value of the herbs you pick."

If you are Chenchen, you can complete this task ?
The first line of the input input has two integers T (1 <= T <= 1000) and M (1 <= M <= 100). T represents the total time that can be used to collect herbs, and M represents the number of herbs in the cave. number. The next M lines each contain two integers between 1 and 100 (inclusive), representing the time when a certain herb was picked and the value of the herb, respectively. Output The output contains only one line, and this line contains only one integer, which represents the maximum total value of herbs that can be harvested within the specified time. sample input
70 3
71 100
69 1
1 2
Sample output
3


simple dp (super) for each

ac:

#include<stdio.h>
#include<string.h>
#include<math.h>

//#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define da 0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// unstoppable header file

int dp[110][1100];
int ti [110], v [110];
int t,m;

int maxx(int a,int b)
{
	return a>b?a:b;
}

intmain()
{
	cin>>t>>m;
	int i,j;
	for(i=0;i<m;++i)
		cin>>ti[i]>>v[i];
	for(i=0;i<m;++i)
	{
		for(j=1;j<=t;++j)
		{
			if(ti[i]<=j)//The time is enough to see if you can choose
				dp[i+1][j]=maxx(dp[i][j-ti[i]]+v[i],dp[i][j]);
			Else//Cannot inherit the previous state
				dp[i+1][j]=dp[i][j];
		}
	}
// for(i=0;i<=m;++i)//debugged code
//	{
//		for(j=0;j<=t;++j)
//			cout<<dp[i][j]<<" ";
//		cout<<endl;
//	}
	cout<<dp[m][t]<<endl;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324792691&siteId=291194637