547B - Mike and Feet - CodeForces Solution


binary search data structures dp dsu *1900

Please click on ads to support us..

C++ Code:

#include<string>
#include<iostream>
#include<vector>
#include<stack>
#include <cstring>
#include<deque>
#include<algorithm>
#include<queue>
#include<set>
#include <unordered_set>
#include<map>
#include<numeric>
#include<cmath>
#include<iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
ll MOD = 1e9 + 7;
ll Pow(ll b, ll p) {
    if (!p) return 1;
    ll ret = Pow(b, p >> 1);
    // ret %= MOD;
    ret *= ret;
    //  ret %= MOD;
    if (p & 1) {
        ret *= b;
        //  ret %= MOD;
    }
    return ret;
}
ll gcd(ll a, ll b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}
#define endl "\n"
#define FaSt cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0)
#define f(a) for(int i=0;i<a;i++)
#define fj(a) for (int j = 0; j < a; j++)
#define RIP return 0;
#define ONES(n) __builtin_popcount(n)
#define wh int v189;cin>>v189;while(v189--)
#define put(a)cout<<a<<endl
#define yea put("YES")
#define Endl endl
#define no put("NO")
#define all(a) a.begin(),a.end()
int dx[] = { +0, +0, -1, +1, +1, +1, -1, -1 };
int dy[] = { -1, +1, +0, +0, +1, -1, +1, -1 };
void solve() {
    int n;
    cin >> n;
    vector<int>v(n),prv(n),after(n),ans(n+1);
    f(n)cin >> v[i];
    stack<int>stk;
    stk.push(-1);
    f(n) {
        while (stk.top() != -1 && v[stk.top()] >= v[i])stk.pop();
        prv[i] = stk.top();
        stk.push(i);
    }
    while (stk.size())stk.pop();
    stk.push(n);
    for (int j = n - 1; j >= 0; j--) {
        while (stk.top() != n && v[stk.top()] >= v[j])stk.pop();
        after[j] = stk.top();
        stk.push(j);
    }
    f(n) {
        int cl = after[i] - prv[i] - 1;
        ans[cl] = max(ans[cl], v[i]);
    }
    for (int i = n - 1; i >= 0; i--) {
        ans[i] = max(ans[i], ans[i+1]);
    }
    for (int i = 1; i <= n; i++)cout << ans[i] << " ";
}

/*


 */

int main() {
    FaSt;
    int t = 1;
    // cin>>t;
    while (t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST