zoukankan      html  css  js  c++  java
  • poj3250

    利用栈进行动态规划,要注意的地方是总和得用longlong因为80000^2是超出int范围的。

    View Code
    #include <cstdio>
    #include
    <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 80002

    struct item
    {
    int num, h;
    }stk[maxn];

    int cow[maxn], f[maxn], front = 0;

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    int n;
    scanf(
    "%d", &n);
    for (int i = 0; i < n; i++)
    scanf(
    "%d", &cow[i]);
    stk[front].num
    = n - 1;
    stk[front
    ++].h = cow[n - 1];
    for (int i = n - 2; i >= 0; i--)
    {
    while (cow[i] > stk[front - 1]. h && front > 0)
    front
    --;
    if (front == 0)
    {
    f[i]
    = n - i - 1;
    stk[front].num
    = i;
    stk[front
    ++].h = cow[i];
    continue;
    }
    f[i]
    = stk[front - 1]. num - i - 1;
    stk[front].num
    = i;
    stk[front
    ++].h = cow[i];
    }
    long long sum = 0;
    for (int i = 0; i < n; i++)
    sum
    += f[i];
    printf(
    "%I64d\n", sum);
    return 0;
    }
  • 相关阅读:
    vsync信号产生与分发
    推荐看过不错的博客及网站
    证明质数有无数个
    242 Valid Anagram
    169 Majority Element
    快速排序--quicksort
    插入排序
    选择排序
    冒泡排序
    指针函数 函数指针 回调函数
  • 原文地址:https://www.cnblogs.com/rainydays/p/1961096.html
Copyright © 2011-2022 走看看