[CF5E/51nod 1482]部落信号

题目大意

给出一个环,环上两点能互相看到的定义是,两条圆弧中任意一条圆弧中间的所经点权值均不大于两点中权值的较小值。
有多少点对能互相看到?

破环为链

找到权值最大的点,然后直接删去它,转为链做。
假如存在i,j能互相看到,且i,j权值均小于权值最大点,则一定不可能通过经过该点的圆弧互相看到。
如果权值等于权值最大点,显然走另一条圆弧也可以互相看到,因此可以删去一个权值最大的点转为链做。
然后考虑所有能互相看到的点对。
i

#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 maxn=1000000+10;
int a[maxn],b[maxn],left[maxn],right[maxn],same[maxn],sta[maxn];
bool bz[maxn];
int i,j,k,l,t,n,m,mx,top;
ll ans;
int read(){
    int x=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
int main(){
    //freopen("1482.in","r",stdin);
    n=read();
    fo(i,1,n){
        a[i]=read();
        if (!mx||a[i]>a[mx]) mx=i;
    }
    fo(i,mx,n) b[++top]=a[i];
    fo(i,1,mx-1) b[++top]=a[i];
    fo(i,1,n) a[i]=b[i];
    top=0;
    fo(i,2,n){
        while (top&&a[i]>=a[sta[top]]) top--;
        left[i]=sta[top];
        sta[++top]=i;
    }
    top=0;
    sta[0]=n+1;
    fd(i,n,2){
        while (top&&a[i]>=a[sta[top]]) {
            if (a[i]==a[sta[top]]) same[i]=same[sta[top]]+1;
            top--;
        }
        right[i]=sta[top];
        sta[++top]=i;
    }
    fo(i,2,n){
        if (left[i]>0) ans++;
        if (right[i]<=n) ans++;
        ans+=(ll)same[i];
    }
    mx=0;
    fo(i,2,n){
        if (mx<=a[i]) bz[i]=1;
        mx=max(mx,a[i]);
    }
    mx=0;
    fd(i,n,2){
        if (mx<=a[i]) bz[i]=1;
        mx=max(mx,a[i]);
    }
    fo(i,2,n) ans+=bz[i];
    printf("%I64d\n",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值