Description

The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determine a race strategy which will get one of them across the finish line as fast as possible.

Like everyone else, cows race bicycles in packs because that's the most efficient way to beat the wind. While travelling at x laps/minute (x is always an integer), the head of the pack expends x*x energy/minute while the rest of pack drafts behind him using only x energy/minute. Switching leaders requires no time though can only happen after an integer number of minutes. Of course, cows can drop out of the race at any time.

The cows have entered a race D (1 <= D <= 100) laps long. Each cow has the same initial energy, E (1 <= E <= 100).

What is the fastest possible finishing time? Only one cow has to cross the line. The finish time is an integer. Overshooting the line during some minute is no different than barely reaching it at the beginning of the next minute (though the cow must have the energy left to cycle the entire minute). N, D, and E are integers.

Input

A single line with three integers: N, E, and D 

Output

A single line with the integer that is the fastest possible finishing time for the fastest possible cow. Output 0 if the cows are not strong enough to finish the race. 

Sample Input

3 30 20

Sample Output

7

Hint

[as shown in this chart:

leader E

pack total used this

time leader speed dist minute

1 1 5 5 25

2 1 2 7 4

3 2* 4 11 16

4 2 2 13 4

5 3* 3 16 9

6 3 2 18 4

7 3 2 20 4

* = leader switch

思路:

1. dp[i][d][e] 表示第 i 头牛使用能量 e 跑 d 圈的最小分钟数假如该牛无法跑完, 则值为 INF.

讨论:

当第 i 头牛领跑时

锁定一头牛后, 假设 dp[d][e] 对应牛使用能量 e 跑 d 圈的最小分钟数, dp[][] 可用完全背包计算

for(int d = 1; d <= D; d++) {
for(int e = 1; e <= E; e++) {
for(int k = 1; k*k <= e && k <= d; k ++) {//完全背包
dp[d][e] = min(dp[d][e], dp[d-k][e-k*k]+1)
}
}
}

第 i 头牛总是作为领跑, 第 i+1 头牛享受 i 头牛的成果

dp[i+1][d][d] = min(dp[i+1][d][d], dp[i][d][e]);

这个状态转移的方程理解为: 当前由第 i 头牛领跑, 第 i+1 头牛享受成果, 即跑了几圈就耗费多少能量. 注意, dp[i][d][e] 中, e >= d, 不管第 i 头牛怎么跑, 只要它跑了 d 圈, i+1 头牛就耗费了 d 的能量.

dp[i+1][d][d] 是对第 i+1 头牛进行初始化, 在代码中也可以看出, 第 i 头牛更新第 i+1 头牛的dp[][][]

总结:

1. 每头牛, 自己跑的时候使用了一次 dp, 牛与牛之间又使用了一次 dp

2. 递推关系仅建立在相邻的两头牛之间, 后面一头牛继承前面那头牛的最小时间

代码:

#include <iostream>
using namespace std; const int INF = 0X3F3F3F3F;
const int MAXN = 30;
int N, E, D;
int dp[30][110][110];
int main() { freopen("E:\\Copy\\ACM\\poj\\1661\\in.txt", "r", stdin);
while(cin >> N >> E >> D) {
for(int i = 0; i <= N; i ++)
for(int j = 0; j <= D; j ++)
for(int k = 0; k <= E; k++)
dp[i][j][k] = INF;
dp[1][0][0] = 0;
for(int i = 1; i <= N; i ++) {
for(int j = 1; j <= D; j++) {
for(int k = 1; k <= E; k++) {
for(int s = 1; s*s <= k && s <= j; s ++) {
dp[i][j][k] = min(dp[i][j][k], dp[i][j-s][k-s*s]+1);
}
dp[i+1][j][j] = min(dp[i+1][j][j], dp[i][j][k]); // 第 i+1 头牛继承第 i 头牛的成果
}
}
}
int ans = INF;
for(int k = 1; k <= E; k ++)
ans = min(ans, dp[N][D][k]);
cout << ans << endl;
}
return 0;
}

  

POJ 1946 Cow Cycling(抽象背包, 多阶段DP)的更多相关文章

  1. POJ 1946 Cow Cycling

    Cow Cycling Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2516   Accepted: 1396 Descr ...

  2. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  4. POJ 2184 Cow Exhibition (01背包的变形)

    本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...

  5. poj 2184 Cow Exhibition(背包变形)

    这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...

  6. POJ 2184 Cow Exhibition 01背包

    题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负   且 sum(x[k]) >= 0 sum(y[k] ...

  7. POJ 2184 Cow Exhibition(背包)

    希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...

  8. poj 1964 Cow Cycling(dp)

    /* 一开始想的二维的 只维护第几只牛还有圈数 后来发现每只牛的能量是跟随每个状态的 所以再加一维 f[i][j][k]表示第i只牛 领跑的j全 已经消耗了k体力 转移的话分两类 1.换一只牛领跑 那 ...

  9. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

随机推荐

  1. 利用eclipse中的各种功能帮助你理解代码

    @菜单栏下面,工具栏,有一对黄色的箭头按钮,一个指向左边,一个指向右边,快捷键是Alt+Left/Alt+Right 功能是跳转到你刚刚编辑过的地方 这里的Left/Right指的是左右方向键,可以方 ...

  2. sqlserver 对多条数据分组

    在开发中,经常会遇到要吧一行行数据按照某一行进行分组 USE [OA] GO /****** Object: StoredProcedure [dbo].[usp_report_GatherDataM ...

  3. BNUOJ-1065或运算的简单解法

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1065 下面有一个程序:--------------------------------------- ...

  4. C#操作word类文件

    最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...

  5. Iconfont在移动端遇到问题的探讨

    Iconfont越来越得到前端的关注,特别是阿里那个iconfont库的推出和不断完善,再加上连IE6都能兼容,的确是个好东西. 既然那么火,我们公司移动项目也计划加入这个iconfont,于是我就针 ...

  6. 測试Service

    <strong><span style="font-size:18px;">自己定义Service:</span></strong> ...

  7. anroid 广播

    广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast().Context.sendOrderedBroa ...

  8. windows 开启端口

    场景: 解决方法: 选中入站规则,右键选择新建入站规则: 然后 选择“允许连接”,点“下一步”. 效果: 连接成功.

  9. Windows 中 .\ 和 ..\ 的区别

    .\ 表示项目文件所在目录之下的目录...\ 表示项目文件所在目录向上一级目录下的目录...\..\表示项目文件所在目录向上二级目录之下的目录.

  10. 关于在SQLITE数据库表中插入本地系统时间的做法

    首先,我参考下面的博文地址:http://blog.csdn.net/liuzhidong123/article/details/6847104 sqlite3 表里插入系统时间(时间戳) 分类: s ...