博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4374:One hundred layer(dp+单调队列优化)
阅读量:6002 次
发布时间:2019-06-20

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

One hundred layer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2172    Accepted Submission(s): 748


Problem Description
Now there is a game called the new man down 100th floor. The rules of this game is:
  
1.  At first you are at the 1st floor. And the floor moves up. Of course you can choose which part you will stay in the first time.
  
2.  Every floor is divided into M parts. You can only walk in a direction (left or right). And you can jump to the next floor in any part, however if you are now in part “y”, you can only jump to part “y” in the next floor! (1<=y<=M)
  
3.  There are jags on the ceils, so you can only move at most T parts each floor.
  
4.  Each part has a score. And the score is the sum of the parts’ score sum you passed by.
Now we want to know after you get the 100th floor, what’s the highest score you can get.
 

Input
The first line of each case has four integer N, M, X, T(1<=N<=100, 1<=M<=10000, 1<=X, T<=M). N indicates the number of layers; M indicates the number of parts. At first you are in the X-th part. You can move at most 
T parts in every floor in only one direction.
Followed N lines, each line has M integers indicating the score. (-500<=score<=500)
 

Output
Output the highest score you can get.
 

Sample Input
 
3 3 2 1 7 8 1 4 5 6 1 2 3
 

Sample Output
 
29
 

Source

题意:N*M的地图,主角初始在1,x位置,每行可以选择仅向左走<=t个位置或仅向右走<=t个位置,然后进入下一行的相应位置,求走到第N行得到的最大值。

思路:从左往右时dp[i][j] = max(dp[i][j], dp[i-1][j-k]+sum[j]-sum[j-k-1] ),k∈[0,t],右往左时依此类推,但纯dp计算会超时,发现每次计算时sun[j]是定值,部分数据可以重复使用,因此可维护一个单调减队列优化时间。

# include 
# include
# include
using namespace std;struct node{ int pos, data;};int map[103][10003], dp[103][10003], sum[10003];int main(){ int n, m, x, t, i, j, ans; struct node tmp; while(~scanf("%d%d%d%d",&n,&m,&x,&t)) { ans = -0x3f3f3f3f; list
p; for(i=1; i<=n; ++i) for(j=1; j<=m; ++j) { scanf("%d",&map[i][j]); dp[i][j] = -0x3f3f3f3f; } dp[1][x] = map[1][x]; for(i=x-1; i>=1&&i>=x-t; --i) dp[1][i] = dp[1][i+1] + map[1][i]; for(i=x+1; i<=m&&i<=x+t; ++i) dp[1][i] = dp[1][i-1] + map[1][i]; for(i=2; i<=n; ++i) { p.clear(); sum[0] = 0; for(j=1; j<=m; ++j) { sum[j] = sum[j-1] + map[i][j]; while(!p.empty() && p.front().pos
=1; --j) { sum[j] = sum[j+1] + map[i][j]; while(!p.empty() && p.front().pos>j+t) p.pop_front(); tmp.pos = j; tmp.data = dp[i-1][j] - sum[j+1]; while(!p.empty() && p.back().data < tmp.data) p.pop_back(); p.push_back(tmp); dp[i][j] = max(dp[i][j], sum[j] + p.front().data); } } for(i=1; i<=m; ++i) ans = max(ans, dp[n][i]); printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6730066.html

你可能感兴趣的文章
怎么关闭或禁用联想ThinkPad笔记本的触摸板
查看>>
linux系统目录分支结构及存放内容
查看>>
毕业设计笔记
查看>>
XMLHttpRequest 及其open()的用法
查看>>
进程与线程的定义以及对多线程、多进程、并发和并行的理解
查看>>
nginx默认虚拟主机、用户认证、域名重定向
查看>>
华为命令二部分
查看>>
2018-03-20
查看>>
LINUX系统重新安装,保留data,以及使用LVM管理
查看>>
袋鼠云数据中台专栏2.0 | 企业三界:业务界面,应用界面,数据界面
查看>>
php 模式设计之单例模式
查看>>
Linux中nginx配置
查看>>
day5 Linux命令
查看>>
oracle的安装步骤
查看>>
Java 并发编程:深入剖析 ThreadLocal
查看>>
安卓手机屏幕投射电脑 手机投屏到win7
查看>>
激光投影机和灯泡投影机的对比
查看>>
Spring Cloud Gateway 入门
查看>>
你是否是一个天生的测试工程师?请对号入座
查看>>
【Linux运维】线上Linux服务器优化经验
查看>>