Digging(DP)
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 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 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 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 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:
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const int SZ = , MAXT = ;
const double eps = 10e-;
struct Coffin
{
int t, s;
double r;
}co[SZ];
int T, N;
int re[MAXT]; bool CMP(const Coffin& x, const Coffin& y)
{
if(fabs(x.r - y.r) > eps) return x.r > y.r;
return x.t < y.t;
} int Max(int a, int b)
{
return a > b ? a : b;
} int main()
{
while(scanf("%d %d", &N, &T) != EOF)
{
for(int i = ; i < N; i++)
{
scanf("%d %d", &co[i].t, &co[i].s);
co[i].r = 1.0 * co[i].s / co[i].t;
}
sort(co, co + N, CMP);
memset(re, , sizeof(re));
int maxRe = ;
for(int i = ; i < N; i++)
{
for(int j = T; j >= co[i].t; j--)
{
re[j] = Max(re[j], re[j - co[i].t] + co[i].s * (T - j + co[i].t));
if(maxRe < re[j]) maxRe = re[j];
}
}
printf("%d\n", maxRe);
}
return ;
}
Digging(DP)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
随机推荐
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- Spring中jdbc Template使用
http://1358440610-qq-com.iteye.com/blog/1826816
- Haproxy + Rabbit 集群 简要介绍
# 两台主机都安装上rabbitMQ yum install -y rabbitmq-server # 两台主机都配置/etc/hosts文件 192.168.23.10 rabbitmq1 19 ...
- 结对编程——paperOne基于java web的简易四则运算出题网站
项目成员:张金生 张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...
- VC的常用调试方法
前言 VS是非常强大的IDE,所以掌握VSVC的常用方法,将会使得我们找出问题解决问题事半功倍. 目录 VSVC的常用调试方法 前言 1. Watch窗口查看伪变量 2. 查看指针指向的一序列值 3. ...
- 有道云笔记web版本居然不支持火狐
好尴尬的火狐呀....
- 【Django】Django—Form两种解决表单数据无法动态刷新的方法
一.无法动态更新数据的实例 1. 如下,数据库中创建了班级表和教师表,两张表的对应关系为“多对多” from django.db import models class Classes(models. ...
- QVariant实质
QVariant实质 QVariant是一种可以存储不同类型的数据结构,在很多场合这是很有用得为了达到这种目的,可以想象,该对象应该存储对象的类型信息,数据信息以及其他辅助详细考虑用途,这种对象必须支 ...
- 【刷题】BZOJ 1951 [Sdoi2010]古代猪文
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...
- BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】
题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...