hdu4374單調佇列+dp
http://acm.hdu.edu.cn/showproblem.php?pid=4374
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.
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)
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
/**
hdu 4374 單調佇列+dp
題目大意:一個n層的樓,每層樓有m個格子,每個格子有一定的價值。在每一層樓只能向一個方向走最多走t步,
開始在頂層的x位置,問到底層的路線上能得到的價值最大。
解題思路:從下往上走,每個狀態dp[i][j]為從i層j格子到底層所能得到的最大價值。
用sum[i][j]表示第i層前j格子的和。
從左邊到j位置的狀態轉移方程 dp[i][j]=max(dp[i+1][k]+sum[i][j]-sum[i][k-1]);(k+t<=j)。
上式即為:dp[i][j]=max(dp[i+1][k]-sum[i][k-1])+sum[i][j+1];
由於max裡面包含的項都已知切與j無關。故可以用單調佇列,維護單調遞增即可。
其右邊的狀態轉移方程為dp[i][j]=max(dp[i+1][k]+sum[i][k])-sum[i][j-1];(k-t>=j)
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int n,m,x,t,a[105][10005],dp[105][10005],sum[105][10005];
int q[10005],front,rear,ans_l[10005],ans_r[10005];
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&x,&t))
{
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&a[i][j]);
sum[i][j]=sum[i][j-1]+a[i][j];
}
}
for(int i=n; i>=1; i--)
{
front=0;
rear=0;
q[rear++]=1;
ans_l[1]=1;
for(int j=2; j<=m; j++)
{
while(front<rear&&dp[i+1][q[rear-1]]-sum[i][q[rear-1]-1]<=dp[i+1][j]-sum[i][j-1])
{
rear--;
}
q[rear++]=j;
if(q[front]+t<j)
front++;
ans_l[j]=q[front];
}
front=0;
rear=0;
q[rear++]=m;
ans_r[m]=m;
for(int j=m-1; j>=1; j--)
{
while(front<rear&&dp[i+1][q[rear-1]]+sum[i][q[rear-1]]<=dp[i+1][j]+sum[i][j])
{
rear--;
}
q[rear++]=j;
if(q[front]-t>j)
front++;
ans_r[j]=q[front];
}
int t1,t2;
for(int j=1; j<=m; j++)
{
t1=dp[i+1][ans_l[j]]+sum[i][j]-sum[i][ans_l[j]-1];
t2=dp[i+1][ans_r[j]]+sum[i][ans_r[j]]-sum[i][j-1];
dp[i][j]=max(t1,t2);
}
}
printf("%d\n",dp[1][x]);
}
return 0;
}
相關文章
- 單調佇列最佳化 DP佇列
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- 單調佇列佇列
- BZOJ1044: [HAOI2008]木棍分割(dp 單調佇列)佇列
- 單調棧/單調佇列佇列
- 單調佇列雙端佇列佇列
- 單調棧 和 單調佇列佇列
- 單調棧和單調佇列佇列
- 聯賽模擬測試18 A. 施工 單調佇列(棧)優化DP佇列優化
- 單調佇列優化dp(五)——#10179. 「一本通 5.5 例 5」Banknotes佇列優化
- 佇列-單端佇列佇列
- 洛谷P4544 [USACO10NOV] Buying Feed G 題解 單調佇列最佳化DP佇列
- noi.ac #289. 電梯(單調佇列)佇列
- 決策單調性DP
- 多重揹包問題的單調佇列優化佇列優化
- RabbitMQ-簡單佇列MQ佇列
- 單向鏈式佇列佇列
- 棧,佇列,優先順序佇列簡單介面使用佇列
- 決策單調性最佳化DP
- python中佇列簡單使用Python佇列
- 佇列_單向連結串列佇列
- laravel 佇列的簡單使用Laravel佇列
- 實現簡單延遲佇列和分散式延遲佇列佇列分散式
- 佇列、阻塞佇列佇列
- kuangbin 專題二十三:二分 尺取 單調棧佇列 String佇列
- 佇列 和 迴圈佇列佇列
- 【佇列】【懶排序】佇列Q佇列排序
- 1284 海港 普及組 NOIP2016 佇列基礎 簡單列舉 簡單模擬 優先佇列(priority_queue)佇列
- 自定義單連結串列佇列的基本介面函式(非迴圈佇列)佇列函式
- 陣列模擬佇列 以及佇列的複用(環形佇列)陣列佇列
- “簡單”的訊息佇列與kafka佇列Kafka
- redis訊息佇列簡單應用Redis佇列
- 簡單易用的任務佇列-beanstalkd佇列Bean
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- 佇列 手算到機算 入門 佇列 迴圈佇列佇列
- 圖解--佇列、併發佇列圖解佇列
- 洛谷題單指南-常見最佳化技巧-P1886 滑動視窗 /【模板】單調佇列佇列
- 佇列佇列
- RabbitMQ 訊息佇列之佇列模型MQ佇列模型