poj 3250 Bad Hair Day

單調隊列,從後往前求單調遞減序列,這樣每個數後面小於自己的數就是其座標與前一個座標的差了。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
struct que
{
   LL  num,id,t;
}q[80010];
LL a[80010],ans;
int main()
{
    int n,i,top,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i = n;i > 0;i --) scanf("%I64d",&a[i]);
        q[0].num = 1000000001;q[0].id = 0;
        top = 0;
        ans = 0;
        for(i = 1;i <= n;i ++)
        {
            for(j = top;j >= 0;j --)
            {
                if(a[i] <= q[j].num) break;
            }
            q[j+1].num = a[i];q[j+1].id = i;
            top = j+1;
            ans += (q[top].id-q[top-1].id-1);
            //for(int  l = 0;l <= top;l ++) cout<<q[l].num<<" ";cout<<endl;
        }
        //cout<<top<<endl;
        printf("%I64d\n",ans);
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章