TZOJ上的C语言作业答案,ZOJ 3689 Digging(C语言版)

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.

0818b9ca8b590ca3270a3433284dd417.png

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 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 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 containsti, 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

这题非常有意思,虽然知道要用到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 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;is[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]max) max=f[i]; printf("%d\n",max); } return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值