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;
}

  

最新文章

  1. C# await和async
  2. 【转】进程与CPU
  3. cvLoadImage函数解析 cvLoadImageM()函数
  4. 串口发送浮点型数据及int(2个字节)long int(4个字节)的方法
  5. 通过 监听器获取sessionId
  6. 该项目中不存在目标 precomputecompiletypescript The target &quot;PreComputeCompileTypeScript&quot; does not exist in the project
  7. cocos2d-x3.2中map的基本操作和使用
  8. tomcat配置多个虚拟主机
  9. String,StringBuilder,StringBuffer
  10. 欲练JS,必先攻CSS——前端修行之路
  11. 安卓高级 特效动画ExplosionField和 SmoothTransition
  12. windows编程 进程的创建销毁和分析
  13. Emacs中的拼写检查
  14. 封装ajax,让调用变得简单优化
  15. 隐藏input光标和输入内容方法
  16. Python练习四
  17. Windows环境安装Django步骤
  18. PHP可变函数
  19. python学习 day4 (3月5日)---列表
  20. mybatis 之 parameterType=&quot;list&quot;

热门文章

  1. Zabbix添加自定义监控项(一)
  2. 使用wireshark抓包工具 检测不到本地网卡
  3. Fastjson 的简单使用&lt;转&gt;
  4. KMP算法匹配原理以及C++实现
  5. C# 关于JArray和JObject封装JSON对象
  6. @RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别
  7. libmysqlclient16 libmysqlclient-dev
  8. svn命令使用常见问题
  9. jquery javascript 回到顶部功能
  10. 试读《基于MVC的JavaScript Web富应用开发》— 不一样的JavaScript