Codeforces Round #305 (Div. 2).D

547B.Mike and Feet

link:http://codeforces.com/contest/548/problem/D

维护一个单调递增的栈, 来记录每个数最大的范围是多少(这样的话要用到map来离散化,但是这样其实是没什么必要的,直接换成i大的范围最大的是多少,这样更快一点)

这代码写的比较挫, 还请见谅.

我其实是故意不写注释的…(逃

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
#define pb push_back
#define lb lower_bound
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N = 200005;
int a[N], n;
map<int, int> mp;
struct node{
    int x;
    int l, r;
    node(){}
    node(int aa, int bb, int cc){x = aa; l = bb; r = cc;}
}stk[N];
int top = 0;
void solve()
{
    for(int i=1; i<=n+1; i++){
        if(top == 0) stk[top++] = (node){a[i], i, i};
        else{
            if(a[i] < stk[top-1].x){
                int tl, tr = stk[top-1].r;
                while(a[i] < stk[top-1].x && top>0){
                    node t = stk[--top];
                    if(mp.find(t.x) == mp.end()) mp[t.x] = 0;
                    mp[t.x] = max(mp[t.x], tr - t.l + 1);
                    tl = t.l;
                }
                stk[top++] = (node){a[i], tl, i};
            }
            else stk[top++] = (node){a[i], i, i};
        }
    }
    map<int, int>::iterator it = mp.end();
    it--;
    for(int i=1; i<=n; i++)
    {
        if(i>1) printf(" ");
        if(it->second >= i) printf("%d", it->first);
        else{
            while(it->second < i && it != mp.begin()) it--;
            printf("%d", it->first);
        }
    }
}
void input()
{
    scanf("%d", &n);
    for(int i=1; i<=n; i++)  scanf("%d", a+i);
    a[n+1] = 0;
    mp.clear();
    solve();
}
int main(void)
{
    #ifdef DK
        freopen("C:\\Users\\dell\\Desktop\\1.in","r",stdin);
    #endif // DK
    input();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值