1272 最大距离(单调栈到底是啥,有几种应用?,归并排序好多用处)

题目描述:

                                         1272 最大距离

 

给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,

则这两个数可以组成一对。每个元素和自己也可以组成一对。例如:{5, 3, 6, 3, 4, 2},

可以组成11对,如下(数字为下标):

(0,0), (0, 2), (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (3, 3), (3, 4), (4, 4), (5, 5)。其中(1, 4)是距

离最大的一对,距离为3。

输入

第1行:1个数N,表示数组的长度(2 <= N <= 50000)。
第2 - N + 1行:每行1个数,对应数组元素Ai(1 <= Ai <= 10^9)。

输出

输出最大距离。

输入样例

6
5
3
6
3
4
2

输出样例

3

思路:

             该题解法较多:

1、stable_sort O(NlogN)排序:

记录所有元素下标,然后按照权值O(NlogN)排序,O(N)遍历,

记录当前最小下标,更新答案。

代码实现:

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
pair<int,int>arr[N];
int main() {
    int n;
    while(cin>>n) {

        for(int i=1; i<=n; i++)cin>>arr[i].first,arr[i].second=i;
        stable_sort(arr+1,arr+1+n); //稳定排序
        int mini=999999999;
        int ans=0;
        for(int i=1; i<=n; i++) {
            mini=min(mini,arr[i].second);
            ans=max(ans,arr[i].second-mini);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

2、O(LogN)单调栈+二分

如果一个元素后面有一个比他大的元素,那后者肯定是可以剔除的,无论如何我们都不会用到他。

然后我们最后留下的元素肯定是递减的,然后就说是单调递减栈?然后如果以栈中最后留下的元素为

序偶对的second,肯定最大只能得到0的答案,而对于那些被剔除的元素,则可以在其被剔除时的栈

中找一个比它小的最远的元素更新答案,这样的时间复杂度是O(NlogN)。(注意这里区别          求小

于该元素的最远元素且中间无比当前元素大的元素        的单调栈应用)。

代码实现:

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
int arr[N],stac[N];
int tot=0;
int main() {
    int n;
    while(cin>>n) {
        tot=0;
        memset(stac,0,sizeof(stac));
        for(int i=1; i<=n; i++)cin>>arr[i];
        int ans=0;
        arr[0]=999999999;
        for(int i=1; i<=n; i++) {
            if(arr[i]>=arr[stac[tot]]) {
                int l=1,r=tot,mid;
                int pos=tot;
                while(l<=r) {
                    mid=(l+r)>>1;
                    if(arr[stac[mid]]>arr[i]) {
                        l=mid+1;
                    } else {
                        r=mid-1;
                        pos=min(pos,mid);
                    }
                }
                pos=stac[pos];
                ans=max(ans,i-pos);
            } else {
                stac[++tot]=i;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

3、O(N)做法,归并排序:

暂时没弄懂。。。。。

只把两个单调序列弄出来了:

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
int arr[N],stac_1[N],stac_2[N];
int tot_1=0,tot_2=0;
int main() {
    int n;
    while(cin>>n) {
        tot_1=0;
        tot_2=0;
        memset(stac_1,0,sizeof(stac_1));
		memset(stac_2,0,sizeof(stac_2));
        for(int i=1; i<=n; i++)cin>>arr[i];
        int ans=0;
        arr[0]=999999999;
        for(int i=1; i<=n; i++) {
            if(arr[i]<arr[stac_1[tot]]) {
               stac_1[++tot]=i;
            } 
        }
        for(int i=n; i>=1; i--) {
            if(arr[i]>arr[stac_2[tot]]) {
               stac_2[++tot]=i;
            } 
        }
	//..............

    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值