Description
When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It’s not difficult to understand why we choose to believe the prophecy (or we just assume it is true to entertain ourselves) if you know the other prophecies appeared in the Maya Calendar. For instance, it has accurately predicted a solar eclipse on July 22, 2009.
The ancient civilization, such as Old Babylonianhas, Ancient Egypt and etc, some features in common. One of them is the tomb because of the influence of the religion. At that time, the symbol of the tomb is the pyramid. Many of these structures featured a top platform upon which a smaller dedicatory building was constructed, associated with a particular Maya deity. Maya pyramid-like structures were also erected to serve as a place of interment for powerful rulers.
Now there are N coffin chambers in the pyramid waiting for building and the ruler has recruited some workers to work for T days. It takes ti days to complete the ith coffin chamber. The size of the ith coffin chamber is si. They use a very special method to calculate the reward for workers. If starting to build the ith coffin chamber when there are t days left, they can get t*si units of gold. If they have finished a coffin chamber, then they can choose another coffin chamber to build (if they decide to build the ith coffin chamber at the time t, then they can decide next coffin chamber at the time t-ti).
At the beginning, there are T days left. If they start the last work at the time t and the finishing time t-ti < 0, they will not get the last pay.
Input
There are few test cases.
The first line contains N, T (1 ≤ N ≤ 3000,1 ≤ T ≤ 10000), indicating there are N coffin chambers to be built, and there are T days for workers working. Next N lines contains ti, si (1 ≤ ti, si ≤ 500).
All numbers are integers and the answer will not exceed 2^31-1.
Output
For each test case, output an integer in a single line indicating the maxminal units of gold the workers will get.
Sample Input
3 10
3 4
1 2
2 1
Sample Output
62
Hint
Start the second task at the time 10
Start the first task at the time 9
Start the third task at the time 6
The answer is 102+94+6*1=62
题意:就是要修建棺材,修建第i个棺材花费的时间为v[i].u,可以获得的报酬是 目前剩余的总时间乘于v[i].w,然后让你求可以获得的最大报酬
思路:我在博客上找了找,方法就是贪心+01背包,发现有两种不同的处理方式,都能AC
第一种
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define M 2000005
using namespace std;
int n,t,dp[10005];
struct uuu
{
int u,w;
}v[10005];
int lsz(uuu a,uuu b)
{
return a.w*b.u>b.w*a.u;
}
int main()
{
while(scanf("%d%d",&n,&t)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int a=0;a<n;a++)
{
scanf("%d%d",&v[a].u,&v[a].w);
}
sort(v,v+n,lsz);
for(int i=0;i<n;i++)
{
for(int j=t;j>=v[i].u;j--)
{
dp[j]=max(dp[j],dp[j-v[i].u]+(t-j+v[i].u)*v[i].w);
}
}
int maxx=0;
for(int a=0;a<=t;a++)
{
if(maxx<dp[a])
{
maxx=dp[a];
}
}
printf("%d\n",maxx);
}
}
第二种(应该更好理解)
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define M 2000005
using namespace std;
int n,t,dp[10005];
struct uuu
{
int u,w;
}v[10005];
int lsz(uuu a,uuu b)
{
return a.w*b.u<b.w*a.u;
}
int main()
{
while(scanf("%d%d",&n,&t)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int a=0;a<n;a++)
{
scanf("%d%d",&v[a].u,&v[a].w);
}
sort(v,v+n,lsz);
for(int i=0;i<n;i++)
{
for(int j=t;j>=v[i].u;j--)
{
dp[j]=max(dp[j],dp[j-v[i].u]+v[i].w*j);
}
}
printf("%d\n",dp[t]);
}
}