利用栈进行动态规划,要注意的地方是总和得用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;
}

相关文章:

  • 2021-12-01
  • 2022-01-18
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2021-11-25
  • 2021-06-12
  • 2021-08-21
  • 2021-10-13
  • 2022-12-23
  • 2022-03-08
相关资源
相似解决方案