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;
}
posted @ 2011-02-22 11:14  金海峰  阅读(391)  评论(0编辑  收藏  举报