zoukankan      html  css  js  c++  java
  • hdu 1506

    对于每一个直方块,只要能知道以它的高度往左(往右)最大能走多少,再枚举一遍所有的直方块即可。而往左(往右)最大能走多少可以用动态规划的方法实现,开数组left(right),对于每一个方块,初始left[i](right[i])为i,然后用迭代的方法往左(右)看,即可求出。思路想出来了,代码就很简单了。

    /*
    * hdu1506/win.cpp
    * Created on: 2011-10-1
    * Author : ben
    */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;

    const int MAXN = 100010;

    int height[MAXN], left[MAXN], right[MAXN];

    void work();
    int main() {
    #ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
    #endif
    work();
    return 0;
    }

    void work() {
    int N;
    long long ans, temp;
    height[0] = -1;
    while (scanf("%d", &N) == 1 && N > 0) {
    for (int i = 1; i <= N; i++) {
    scanf("%d", &height[i]);
    left[i] = i;
    right[i] = i;
    }
    height[N + 1] = -0x7fffffff;
    for (int i = 1; i <= N; i++) {
    while (height[left[i] - 1] >= height[i]) {
    left[i] = left[left[i] - 1];
    }
    }
    for (int i = N; i >= 1; i--) {
    while (height[right[i] + 1] >= height[i]) {
    right[i] = right[right[i] + 1];
    }
    }
    ans = -1;
    for (int i = 1; i <= N; i++) {
    temp = right[i] - left[i] + 1;
    temp *= height[i];
    if (temp > ans) {
    ans = temp;
    }
    }
    printf("%I64d\n", ans);
    }
    }



  • 相关阅读:
    类的加载过程 以及实例的加载顺序
    设计优化之单例模式
    Java程序性能优化之性能概述
    1-12接口
    1-11多态
    1-9方法的重写(override)
    1-6static关键字
    1-10super和this关键字
    1-8继承extends
    1-7代码块的分类
  • 原文地址:https://www.cnblogs.com/moonbay/p/2197091.html
Copyright © 2011-2022 走看看