Codeforces5E - Bindian Signalizing

本文介绍了一种解决环形山脉视线问题的算法,通过将环转换为链并使用动态规划来确定每座山左右两侧可见山的数量。该算法利用递归更新左侧和右侧首个高于当前山的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

先把这个环变成一个链,即把最高的山作为第一个山,然后求出每一座山左边和右边第一个高于本身的位置,并且求出本位置到右边第一个高于本身的位置中一样高度的山。

用一种动态规划的想法大约线性多一点的时间求出left和right还有c数组。然后每一个座山只要左,右边有比他高的,那么就是两种,要么就只有一种

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define Max(a,b) a>b?a:b
const int maxn=1000005;
int a[maxn],b[maxn],lef[maxn],rig[maxn],c[maxn]={0};
int main()
{
    int n;
    scanf("%d",&n);
    int ma=-1,mid=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>ma)
        {
            ma=a[i];
            mid=i;
        }
    }
    mid--;
    for(int j=1;j<=n;j++)
        b[j]=a[(mid+j)%n];
    lef[1]=1;
    for(int i=2;i<=n;i++)
    {
        lef[i]=i-1;
        while(lef[i]>1&&b[lef[i]]<=b[i])
            lef[i]=lef[lef[i]];//往左递归
    }
    for(int i=n;i>=1;i--)
    {
        rig[i]=i+1;
        while(rig[i]<=n&&b[rig[i]]<b[i])
            rig[i]=rig[rig[i]];//往右递归
        if(rig[i]<=n&&b[rig[i]]==b[i])
        {
            c[i]=c[rig[i]]+1;
            rig[i]=rig[rig[i]];
        }
    }
    long long ans=0;
    for(int i=2;i<=n;i++)
    {
        ans+=c[i]+2;
        if(lef[i]==1&&rig[i]==n+1)
            ans--;
    }
    printf("%lld\n",ans);
}


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值