HDOJ 1176 免费馅饼 -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1176

为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。
6
5 1
4 1
6 1
7 2
7 2
8 3
0
4
状态pie[i][j]表示i时刻在坐标j出最多能接到的馅饼数。
状态转移方程:
pie[i][j] = Max(pie[i+1][j-1], pie[i+1][j], pie[i+1][j+1]) + pie[i][j].
最后pi[0][5]即为所求结果。
#include <stdio.h>
#include <string.h>
#include <math.h> #define MAX 100001 int pie[MAX][12]; /*pie[i][j]表示在i时刻落在j点的馅饼数量*/ int MaxOf2(int a, int b){
return (a > b) ? a : b;
} int MaxOf3(int a, int b, int c){
int max = (a > b) ? a : b;
return (max > c) ? max : c;
} int MaxNumOfPie(int max_time){
int i, j, max;
for (i = max_time - 1; i >= 0; --i){
pie[i][0] = MaxOf2(pie[i+1][0], pie[i+1][1]) + pie[i][0];
/*printf("%d ", pie[i][0]);*/
for (j = 1; j < 10; ++j){
pie[i][j] = MaxOf3(pie[i+1][j-1], pie[i+1][j], pie[i+1][j+1]) + pie[i][j];
/*printf ("%d ", pie[i][j]);*/
}
pie[i][10] = MaxOf2(pie[i+1][10], pie[i+1][9]) + pie[i][10];
/*printf ("%d\n", pie[i][10]);*/
}
return pie[0][5];
} int main(void){
int n;
int i;
int time;
int location;
int max_time; while (scanf("%d", &n) != EOF && n != 0){
memset(pie, 0, sizeof(pie));
max_time = -1;
for (i=1; i<=n; ++i){
scanf ("%d%d", &location, &time);
++pie[time][location];
if (max_time < time)
max_time = time;
}
printf ("%d\n", MaxNumOfPie(max_time));
} return 0;
}
HDOJ 1176 免费馅饼 -- 动态规划的更多相关文章
- Hdoj 1176 免费馅饼 【动态规划】
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDOJ --- 1176 免费馅饼
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Hdoj 1176.免费馅饼 题解
Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...
- hdu 1176 免费馅饼(动态规划)
AC code: #include<stdio.h> #include<string.h> #define max(a,b) (a>b?a:b) #define maxo ...
- HDOJ 1176 免费馅饼(完全背包)
参考:https://blog.csdn.net/hhu1506010220/article/details/52369785 https://blog.csdn.net/enjoying_scien ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
- hdu 1176 免费馅饼(数塔类型)
http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1176 免费馅饼【动态规划】
解题思路:用a[i][j]表示在第i秒在地点j的掉落馅饼的数量,设整个馅饼掉落的时间持续为timemax,即为矩阵的高度,一共0到10个地点,为矩阵的长度,如图,即可构成数塔,因为考虑到在地点0的时候 ...
随机推荐
- squid3.0 隐藏 hearder 设置
原文地址: http://www.wenzizone.cn/?p=311 squid2.6可以使用header_access来控制显示与隐藏http的header信息,但是在squid3.0版本中这个 ...
- 【原创】OllyDBG 入门系列(一)-认识OllyDBG
****** http://blog.fishc.com/645.html 标 题: [原创]OllyDBG 入门系列(一)-认识OllyDBG作 者: CCDebuger时 间: 2006-0 ...
- Web版RSS阅读器(三)——解析在线Rss订阅
上篇博客<Web版RSS阅读器(二)——使用dTree树形加载rss订阅分组列表>已经写到读取rss订阅列表了,今天就说一下,当获取一条在线rss订阅的信息,怎么去解析它,从而获取文章或资 ...
- [Practical Git] Filter commit history with git log arguments
In the last lesson, we learned how to format the git log output; in this lesson we will learn how to ...
- android学习日记06--View视图
一.android 界面开发 1.三个重要的类:View视图.Canvas画布.Paint画笔2.android 界面开发常用三种视图 View --只能在主线程中更新,没有缓存 ...
- Google maps API开发(一)(转)
一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...
- Object.defineProperty vs __defineGetter__ vs normal
Testing in Chrome 31.0.1650.63 32-bit on Windows Server 2008 R2 / 7 64-bit Test Ops/sec Object.defin ...
- innodb_strict_mode=1
从MySQL5.5.X版本开始,你可以开启InnoDB严格检查模式,尤其采用了页数据压缩功能后,最好是开启该功能.开启此功能后,当创建表(CREATE TABLE).更改表(ALTER TABLE)和 ...
- Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...
- c#_delegate_异步调用_BeginInvoke
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...