码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Beta Round #5 E. Bindian Signalizing

时间:2015-02-17 21:04:08      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:dp

题目大意

有N座山组成一个环,两座山互相能看到的要求是相连的圆弧上没有任何其他的山高度比它们高。求能看到的山的组数。

解题思路

首先要拆环成链,将山的序列改变,第一座山是最高的山。
其次是统计对于这个序列的L数组和R数组。表示:

  • First hill to the left of the x, which is strictly higher than x.
  • First hill to the right of the x, which is strictly higher than x.

生成C数组:

  • All hills that are as high as x and are located between x and y.

(来自官方题解)

最后统计答案。

题目代码

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <time.h>
#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
using namespace std;


const int N = 1000005;

int num[N],l[N],r[N],c[N],temp[N];


int main()
{
    int n,max1=-1,p=-1;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&temp[i]);
        if(max1<temp[i])
        {
            max1=temp[i];
            p=i;
        }
    }
    num[n]=temp[p];
    for(int i=0; i<n; i++)
    {
        num[i]=temp[p++];
        p%=n;
        //cout<<num[i]<<" ";
    }

    for(int i=1;i<n;i++)
    {
        l[i]=i-1;
        while(l[i]>0&&num[i]>=num[l[i]])
        {
            l[i]=l[l[i]];
        }
    }

    c[n]=0;
    for(int i=n-1;i>=0;i--)
    {
        r[i]=i+1;
        while(r[i]<n&&num[i]>num[r[i]])r[i]=r[r[i]];
        if(r[i]<n&&num[i]==num[r[i]])
        {
            c[i]=c[r[i]]+1;
            r[i]=r[r[i]];
        }
    }
    long long ans=0;
    for(int i=1;i<n;i++)
    {
        ans+=c[i];
        ans+=2;
        if(l[i]==0&&r[i]==n)ans--;
    }
    cout<<ans;
    return 0;
}

Codeforces Beta Round #5 E. Bindian Signalizing

标签:dp

原文地址:http://blog.csdn.net/lin375691011/article/details/43867891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!