zoukankan      html  css  js  c++  java
  • HDU-4374 One hundred layer (单调队列 + dp )

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

    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

    解题思路

    明显可以看出dp式
    dp[i][j] = dp[i - 1][k] + sum[i][j] - sum[i][k - 1];
    dp[i][j] = dp[i - 1][k] + sum[i][k] - sum[i][j - 1];
    但是由于 k太大了直接做会超时,由于sum[i][j]和sum[i][j - 1]都是常数, 所以利用单调队列来维护
    dp[i - 1][k] - sum[i][k - 1] 和 dp[i - 1][k] + sum[i][k]

    #define _CRT_SECURE_NO_WARNINGS
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <cstring>
    #include <string>
    #include <map>
    #include <stack>
    #define M 1000000
    #define ll long long 
    #define se second
    #define fi first
    #define pb push_back
    #define INF 0x3f3f3f3f
    #define de(x) cout << #x << " = "<< x << endl;
    #define eps 1e-6
    using namespace std;
    int n, m, x, t;
    int dp[105][10005], a[105][10005], sum[105][10005];
    deque <pair<int, int > > q;
    int main()
    {
    	while (cin >> n >> m >> x >> t)
    	{
    		memset(sum, 0, sizeof(sum));
    		memset(dp, 0, sizeof(dp));
    		memset(a, 0, sizeof(a));
    		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 = 0; i <= n; i++)
    			for (int j = 0; j <= m; j++) dp[i][j] = -INF;
    		dp[0][x] = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			q.clear();
    			for (int j = 1; j <= m; j++)
    			{
    				if (q.size() && j - q.front().second > t) q.pop_front();
    				while (q.size() && q.back().first <= (dp[i - 1][j] - sum[i][j - 1]))
    				{
    					q.pop_back();
    				}
    				q.push_back({ dp[i - 1][j] - sum[i][j - 1] , j });
    				dp[i][j] = q.front().first + sum[i][j];
    			}
    			q.clear();
    			for (int j = m; j > 0; j--)
    			{
    				if (q.size() && q.front().second - j > t) q.pop_front();
    				while (q.size() && q.back().first <= (dp[i - 1][j] + sum[i][j]))
    				{
    					q.pop_back();
    				}
    				q.push_back({ dp[i - 1][j] + sum[i][j] , j });
    				dp[i][j] = max(dp[i][j], q.front().first - sum[i][j - 1]);
    			}
    		}
    		int maxx = -INF;
    		for (int i = 1; i <= m; i++) maxx = max(dp[n][i], maxx);
    		printf("%d
    ", maxx);
    	}
    	return 0;
    }
    
  • 相关阅读:
    第三周学习进度条
    绘制echarts折线图
    第二周学习进度条
    返回一个整数数组中最大子数组的和
    软件工程第二周开课博客
    第一周学习进度条
    学习进度
    学习进度
    学习进度
    HDU 4906 (dp胡乱搞)
  • 原文地址:https://www.cnblogs.com/seast90/p/9382411.html
Copyright © 2011-2022 走看看