codeforces1156E. Special Segments of Permutation(单调栈)

                      E. Special Segments of Permutation

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given a permutation pp of nn integers 11, 22, ..., nn (a permutation is an array where each element from 11 to nn occurs exactly once).

Let's call some subsegment p[l,r]p[l,r] of this permutation special if pl+pr=maxi=lrpipl+pr=maxi=lrpi. Please calculate the number of special subsegments.

Input

The first line contains one integer nn (3≤n≤2⋅1053≤n≤2⋅105).

The second line contains nn integers p1p1, p2p2, ..., pnpn (1≤pi≤n1≤pi≤n). All these integers are pairwise distinct.

Output

Print the number of special subsegments of the given permutation.

Examples

input

Copy

5
3 4 1 5 2

output

Copy

2

input

Copy

3
1 3 2

output

Copy

1

Note

Special subsegments in the first example are [1,5][1,5] and [1,3][1,3].

The only special subsegment in the second example is [1,3][1,3].

 

 

一、原题地址

点我传送

 

二、大致题意

给定一个 [ 1 , n ]的序列,所有的数字不重复。

询问的是满足:这个式子的( l , r )有多少对。

 

三、大致思路

利用单调栈维护出位于 i 位置上的数字是最大值时,能延伸到的最远的左端点和右端点。复杂度是nlogn。

然后对于每个位置 i  的贡献,我们是需要在他的左区间和右区间上各取一个数,让他们的和等于 i 位置上的数,那么枚举位置 i ,统计贡献时,我们取范围较小的一侧作为枚举的对象,这样复杂度最糟糕的情况就是nlogn,是可以接受的。

 

四、代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#include <vector>
#include <set>
#include<stack>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;

const int N=2e5+5;
int n;
LL ans;
struct Node
{
    int val,p;
    Node(){}
    Node(int _val,int _p)
    {
        val=_val;p=_p;
    }
}a[N];
stack<Node>stc;
int toL[N],toR[N],pos[N];

void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].val);
        a[i].p=i;
    }
    return ;
}

void init()     //利用单调栈维护左右能达到的最大位置
{               //并且记录每个数字的位置
    stc.push(Node(inf,0));
    toL[1]=0;
    for(int i=1;i<=n;i++)
    {
        pos[a[i].val]=i;        //记录每个数字的位置
        while(stc.top().val<a[i].val)stc.pop();
        toL[i]=stc.top().p;
        stc.push(Node(a[i].val,i));
    }
    //初始化左边能达到的最大值

    while(!stc.empty())stc.pop();

    stc.push(Node(inf,n+1));
    toL[n]=n+1;
    for(int i=n;i>=1;i--)
    {
        while(stc.top().val<a[i].val)stc.pop();
        toR[i]=stc.top().p;
        stc.push(Node(a[i].val,i));
    }
    //初始化右边能达到的最大值
    ans=0;return ;
}

void work(int l,int r,int ql,int qr,int nowmax)
{
    for(int i=l;i<=r;i++)
    {
        int need=nowmax-a[i].val;
        if(pos[need]>=ql&&pos[need]<=qr)ans++;
    }
    return ;
}

int main(void)
{
    read();
    init();
    for(int i=1;i<=n;i++)
    {
        int left=i-toL[i]-1;
        int right=toR[i]-i-1;
        if(left==0||right==0)continue;
        if(left<right)work(toL[i]+1,i-1,i+1,toR[i]-1,a[i].val);
        else work(i+1,toR[i]-1,toL[i]+1,i-1,a[i].val);
    }
    printf("%lld\n",ans);
}

 

单调栈是一种常用的数据结构,用于解决一类特定的问题,其中最常见的问题是找到数组中每个元素的下一个更大或更小的元素。在Codeforces编程竞赛中,单调栈经常被用于解决一些与数组相关的问题。 下面是单调栈的一般思路: 1. 创建一个空栈。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将当前元素入栈。 4. 重复步骤3,直到遍历完所有元素。 这样,最后剩下的栈中元素就是没有下一个更大或更小元素的元素。在使用单调栈求解具体问题时,我们可以根据需要进行一些特定的操作。 例如,如果要找到一个数组中每个元素的下一个更大的元素,可以使用单调递减栈。具体操作如下: 1. 创建一个空栈和一个空结果数组。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将其在结果数组中的位置记录为当前元素的下一个更大元素的索引。 4. 将当前元素入栈。 5. 重复步骤3和4,直到遍历完所有元素。 6. 结果数组中没有下一个更大元素的位置,可以设置为-1。 以下是一个使用单调递减栈求解下一个更大元素问题的示例代码: ```cpp #include <iostream> #include <stack> #include <vector> std::vector<int> nextGreaterElement(std::vector<int>& nums) { int n = nums.size(); std::vector<int> result(n, -1); std::stack<int> stack; for (int i = 0; i < n; i++) { while (!stack.empty() && nums[i] > nums[stack.top()]) { result[stack.top()] = i; stack.pop(); } stack.push(i); } return result; } int main() { std::vector<int> nums = {1,3, 2, 4, 5, 1}; std::vector<int> result = nextGreaterElement(nums); for (int i = 0; i < result.size(); i++) { std::cout << "Next greater element for " << nums[i] << ": "; if (result[i] != -1) { std::cout << nums[result[i]]; } else { std::cout << "None"; } std::cout << std::endl; } return 0; } ``` 以上代码将输出: ``` Next greater element for 1: 3 Next greater element for 3: 4 Next greater element for 2: 4 Next greater element for 4: 5 Next greater element for 5: None Next greater element for 1: None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值