题解:

发现是一个环,而环的题目有一些就是要转化成为链

首先找到一个最高点,中间断开

然后当作一条链来做

代码:

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int N=+;
int a[N],b[N],left[N],right[N],same[N],sta[N],bz[N];
int i,j,k,l,t,n,m,mx,top;
ll ans;
int read()
{
int x=;
char ch=getchar();
while (ch<''||ch>'') ch=getchar();
while (ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x;
}
int main()
{
n=read();
fo(i,,n)
{
a[i]=read();
if (!mx||a[i]>a[mx]) mx=i;
}
fo(i,mx,n) b[++top]=a[i];
fo(i,,mx-) b[++top]=a[i];
fo(i,,n) a[i]=b[i];
top=;
fo(i,,n)
{
while (top&&a[i]>=a[sta[top]]) top--;
left[i]=sta[top];
sta[++top]=i;
}
top=;
sta[]=n+;
fd(i,n,)
{
while (top&&a[i]>=a[sta[top]])
{
if (a[i]==a[sta[top]]) same[i]=same[sta[top]]+;
top--;
}
right[i]=sta[top];
sta[++top]=i;
}
fo(i,,n)
{
if (left[i]>) ans++;
if (right[i]<=n) ans++;
ans+=(ll)same[i];
}
mx=;
fo(i,,n)
{
if (mx<=a[i]) bz[i]=;
mx=max(mx,a[i]);
}
mx=;
fd(i,n,)
{
if (mx<=a[i]) bz[i]=;
mx=max(mx,a[i]);
}
fo(i,,n) ans+=bz[i];
printf("%I64d\n",ans);
}

51nod1482的更多相关文章

  1. [51nod1482]部落信号 单调栈

    ~~~题面~~~ 题解: 可以发现这是一道单调栈的题目,首先来考虑数字没有重复时如何统计贡献. 因为这是一个环,而如果我们从最高的点把环断开,并把最高点放在链的最后面(顺时针移动),那么因为在最高点两 ...

随机推荐

  1. 我在linux中使用的vundle 和 vimrc配置

    set nocompatible filetype off set rtp+=~/.vim/bundle/vundle/ call vundle#rc() Plugin 'gmarik/vundle' ...

  2. C++作业:Circle_area

    Github链接: Circle_area 代码: main.cpp #include "circle_area.h" #include <iostream> #inc ...

  3. 03_Spark集群部署

    [安装前的环境准备] Hadoop:2.6.1Java:jdk-1.7.0Spark: spark-1.6.0-bin-hadoop2.6.tgzScala: scala-2.11.4.tgz虚拟机: ...

  4. Linux-Ubuntu14.04下mongodb安装部署

    mongo 下载:https://www.mongodb.com/download-center/community?jmp=nav 创建目录,将下载的文件放在如下位置 xxx@ubuntu:/usr ...

  5. android调用照相机拍照获取照片并做简单剪裁

    引用转载http://www.cnblogs.com/eyu8874521/archive/2012/07/20/2600697.html 效果: 客服端代码:   package com.cn.lx ...

  6. Codeforces 898F - Restoring the Expression(字符串hash)

    898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...

  7. C# 通过Newtonsoft.Json.dll序列化日期的处理

    Newtonsoft.Json.dll提供了非常好的Json序列化和反序列化方式,但是对日期的处理却让我纠结了很久.首先定义类如下: public class Student{ public int ...

  8. 在linux环境下编译C++ 程序

    单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: 单个源文件生成可执行程序 /* helloworld.cpp */ #include ...

  9. protected internal == internal

    总结:在同程序集下,protected internal类型修饰的成员变量可以在基类或派生类的类内.类外访问(同程序集下protected internal和internal访问性质相同,此处保留了i ...

  10. 3.3 x86指令简介

    计算机组成 3 指令系统体系结构 3.3 x86指令简介 x86指令种类繁多,数量庞大.在这一节我们将会学习x86指令的分类,并分析其中最为基础的一部分指令. 通常一个指令系统主要包括这几类指令.运算 ...