CodeForces - 548D Mike and Feet(单调栈)

题意:给出n个数,假设区间长度为k,给定一个起点求区间最小值,由于有很多起点,所以要求的是这些的最大值。然后k的范围是1-n。。(感觉好别扭)。所以要输出n个答案。

做法:我们都知道单调栈可以处理出一个数为最小值的最长区间,那么先处理出这个东西。再把数字带着他的最长区间降序排个序。设指针p为1,我们可以发觉对于区间长度从小到大来说答案是不增的,所以如果当前数字最长区间为x,那么我们只需要更新p到x为这个数。因为小于p的区间长度已经是当前数字之前数字为答案了(比当前大)。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 10000000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
const int maxn = 200000+10;
int st[maxn][2],a[maxn];
struct node
{
    int val,ran;
    bool operator < (const node &t) const
    {
        return val > t.val;
    }
}no[maxn];
int l[maxn],r[maxn];
int ans[maxn];
int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        no[i].val = a[i];
    }
    int top = 0;
    st[0][0] = st[0][1] = 0;
    for(int i = 1; i <= n; i++)
    {
        while(a[i] <= st[top][0]) top--;
        l[i] = st[top][1]+1;
        st[++top][0] = a[i];
        st[top][1] = i;
    }
    top = 0;
    st[0][0] = 0;
    st[0][1] = n+1;
    for(int i = n; i >= 1; i--)
    {
        while(a[i] <= st[top][0]) top--;
        r[i] = st[top][1]-1;
        st[++top][0] = a[i];
        st[top][1] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        no[i].ran = r[i]-l[i]+1;
    }
    sort(no+1,no+1+n);
    int p = 1;
    for(int i = 1; i <= n; i++)
    {
        if(no[i].ran < p) continue;
        while(p <= no[i].ran)
            ans[p++] = no[i].val;
    }
    for(int i = 1; i <= n; i++)
        printf("%d%c",ans[i],(i==n)?'\n':' ');
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值