ZOJ 3689 Digging(C语言版)

来源:互联网 发布:mysql 修改字段默认值 编辑:程序博客网 时间:2024/06/08 17:38
Digging

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 BabylonianhasAncient 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 ithcoffin 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 NT (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 containstisi (1 ≤ tisi ≤ 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 103 41 22 1

Sample Output

62

这题非常有意思,虽然知道要用到01背包,但是还是很难做出。

因为所取的物品按不同的顺序排放会得到不同的价值。

思路是先将各物品排好顺序,再用01背包考虑取和不取。(这里的物品为一个工程)

用t[i],s[i]表示这个工程的耗时和工程面积。

考虑任意两个相邻位置的工程,注意到这两个工程交换位置不会影起其它工程所获价值的变化。

那么假设此时剩l天。value1=l*s[i]+(l-t[i])*s[i+1];

若交换这两个工程:value2=l*s[i+1]+(l-t[i+1])*s[i];

value2-value1=t[i]*s[i+1]-t[i+1]*s[i];

若这个差大于0,则表明安排工程时应将i+1这个工程放在i前面。理解为工程i+1比i更有价值。

所以我们在选择前把所有的工程按“价值”的从高到低排序。

再用01背包考虑最优解。

这里需要注意的是因为我们把“高价值”的工程先考虑,所以理解背包dp方程时

f[v-c[i]]理解为在天数为v-c[i]天的工程表“后”插入该工程。(这句话仔细理解)

普通的01背包没有考虑在放入物品时背包的状态,而此题需要我们弄清。

贴上AC代码:

#include<stdio.h>
int f[10001];
int main()
{
int v,n,T,t[3001],s[3001],i,j,k,temp,max;
while(scanf("%d%d",&n,&T)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d%d",&t[i],&s[i]);
for(i=1;i<=T;i++)
f[i]=0;
for(i=1;i<n;i++)
{
for(j=1;j<=n-i;j++)
{
k=j+1;
if(t[j]*s[k]>s[j]*t[k])
{
temp=t[j];
t[j]=t[k];
t[k]=temp;
temp=s[j];
s[j]=s[k];
s[k]=temp;
}
}
}
for(i=1;i<=n;i++)
{
for(v=T;v>=t[i];v--)
{
j=f[v-t[i]]+(T-v+t[i])*s[i];//(T-v+t[i])表示剩余天数,因为此工程插在(v-c[i])天的工程表后。
if(f[v]<j)
f[v]=j;
}
}
max=0;
for(i=0;i<=T;i++)//最后需要遍历一下是因为物品的插入位置不确定。
if(f[i]>max)
max=f[i];
printf("%d\n",max);
}
return 0;
}

原创粉丝点击