博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Digging(DP)
阅读量:6966 次
发布时间:2019-06-27

本文共 3616 字,大约阅读时间需要 12 分钟。

ZOJ Problem Set - 3689
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 tidays 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 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 contains tisi (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

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 10*2+9*4+6*1=62

可以转化为01背包问题解决,物品为要修建的墓地,要修建的墓地数N为物品数,总天数T是背包容量,物品价值为t*si,物品体积为ti。注意传统的01背包问题中,物品的价值是固定的,但是该题中物品的价值t*si受t影响,是不固定的,换言之,物品的价值是可分配的,因此要先确定物品价值分配的最优策略,在此基础上再用01背包问题求解。

 方案:按照si/ti对物品进行排序,在进行01背包算法时按此顺序遍历物品,能保证所取物品中,si/ti越大,分配的剩余时间t也越大,那么si*t/ti越大,si*t/ti就是物品i单位体积的价值,一个背包中物品的单位体积的平均价值越大,在背包容量固定为T的情况下,自然能获得最优解。

AC Code:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 11 using namespace std;12 13 const int SZ = 30005, MAXT = 10003;14 const double eps = 10e-5;15 struct Coffin16 {17 int t, s;18 double r;19 }co[SZ];20 int T, N;21 int re[MAXT];22 23 bool CMP(const Coffin& x, const Coffin& y)24 {25 if(fabs(x.r - y.r) > eps) return x.r > y.r;26 return x.t < y.t;27 }28 29 int Max(int a, int b)30 {31 return a > b ? a : b;32 }33 34 int main()35 {36 while(scanf("%d %d", &N, &T) != EOF)37 {38 for(int i = 0; i < N; i++)39 {40 scanf("%d %d", &co[i].t, &co[i].s);41 co[i].r = 1.0 * co[i].s / co[i].t;42 }43 sort(co, co + N, CMP);44 memset(re, 0, sizeof(re));45 int maxRe = 0;46 for(int i = 0; i < N; i++)47 {48 for(int j = T; j >= co[i].t; j--)49 {50 re[j] = Max(re[j], re[j - co[i].t] + co[i].s * (T - j + co[i].t));51 if(maxRe < re[j]) maxRe = re[j];52 }53 }54 printf("%d\n", maxRe);55 }56 return 0;57 }

 

转载地址:http://jyisl.baihongyu.com/

你可能感兴趣的文章
Spark MLlib编程API入门系列之特征提取之主成分分析(PCA)
查看>>
ionic3 app 退出应用程序
查看>>
Android 国际区号注册手机号编码 以及常用城市列表
查看>>
android 添加新的键值,自定义按键-2【转】
查看>>
【转】C++拷贝构造函数详解
查看>>
复旦大学高等代数考试命题的若干经验
查看>>
自主学习Flappy Bird游戏
查看>>
gitlab pipelines 使用
查看>>
哪些情况下索引会失效?
查看>>
UWP开发随笔——UWP新控件!AutoSuggestBox!
查看>>
Nginx+mysql+php(待补全)
查看>>
75.培训管理-培训信息发布 Extjs 页面
查看>>
[转]使用rosbridge协议实现安卓跟ros的解耦
查看>>
maven添加jetty插件,同时运行多个实例
查看>>
洛谷 P3807 【模板】卢卡斯定理
查看>>
mac使用phpize进行安装的时候碰到的问题
查看>>
前端插件实现图片懒加载
查看>>
jquery绑定onkeyup()事件3中方法
查看>>
正则表达式总结
查看>>
WPS或xls 数据分列 清洗
查看>>