题解:

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

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

然后当作一条链来做

代码:

#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. UNIX网络编程--简介(一)【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/11760187 一.概述 a) 在编写与计算机通信的程序时,首先要确定的就是和计算机通信的 ...

  2. Linux内核分析--内核中的数据结构双向链表续【转】

    在解释完内核中的链表基本知识以后,下面解释链表的重要接口操作: 1. 声明和初始化 实际上Linux只定义了链表节点,并没有专门定义链表头,那么一个链表结构是如何建立起来的呢?让我们来看看LIST_H ...

  3. windows10下cygwin安装神器apt-cyg

    一.背景 需要在cygwin下安装一些库 二.安装 2.1获取apt-cyg源码 git clone https://github.com/transcode-open/apt-cyg.git 2.2 ...

  4. <offer4> 04_FindInPartiallySortedMatrix

    #include<cstdio> bool Find(int* matrix, int rows, int columns, int number) { bool result = fal ...

  5. [BZOJ3609][Heoi2014]人人尽说江南好 结论题

    Description 小 Z 是一个不折不扣的 ZRP(Zealot Round-game Player,回合制游戏狂热玩家), 最近他 想起了小时候在江南玩过的一个游戏.     在过去,人们是要 ...

  6. 文件查找:locate、find

    文件查找:在文件系统上查找符合条件的文件: locate, find 非实时查找(数据库查找):locate  //不是遍历系统文件,把当前系统目录下的所有文件抽取出来制作成一个索引(或者叫数据库), ...

  7. HDU 6069 Counting Divisors(唯一分解定理+因子数)

    http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 思路: 根据唯一分解定理,$n={a_{1}}^{p1}*{a2_{}}^{p2}...*{a_{ ...

  8. python后端工程师 数据爬虫

    大数据挖掘分析平台和产品的建设. 工作职责: 独立完成软件系统代码的设计与实现: 根据需求完成设计,代码编写,调试,测试和维护: 使用Python 语言对后台业务逻辑进行开发以及维护: 能根据实际需求 ...

  9. ubuntu 14.04 安装 gflags

    1.下载 git clone https://github.com/gflags/gflags 2.编译 进入源码目录(即gflags文件夹) cmake . make -j 24 sudo make ...

  10. Unix网络编程_卷1卷2

    1. UNIX 网络编程(第2版)第1卷:套接口API和X/Open 传输接口API PDFhttp://www.linuxidc.com/Linux/2014-04/100155.htm UNIX网 ...