Cod sursa(job #3354681)

Utilizator alex_0747Gheorghica Alexandru alex_0747 Data 19 mai 2026 19:22:05
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.72 kb
#include <bits/stdc++.h>
using namespace std;

class NearestSmallestValues
{
public:
    static vector<int> solve(vector<int> &v)
    {
        stack<int> st;
        vector<int> ans;
        size_t n = v.size();
        for (size_t i = 0; i < n; i++) {
            while (!st.empty() && v[st.top()] >= v[i]) {
                st.pop();
            }
            if (st.empty()) {
                ans.push_back(0);
            } else {
                ans.push_back(st.top() + 1);
            }
            st.push(i);
        }
        return ans;
    } 
};

class Strabunica
{
public:
    static long long solve(vector<int> &v)
    {
        stack<int> st;
        size_t n = v.size();
        vector<int> l(n), r(n);
        long long val;
        for (size_t i = 0; i < n; i++) {
            while (!st.empty() && v[st.top()] >= v[i]) {
                st.pop();
            }
            if (st.empty()) {
                l[i] = 0;
            } else {
                l[i] = st.top() + 1;
            }
            st.push(i);
        } 
        while (!st.empty()) {
            st.pop();
        }
        for (int i = n - 1; i >= 0; i--) {
            while (!st.empty() && v[st.top()] >= v[i]) {
                st.pop();
            }
            if (st.empty()) {
                r[i] = n + 1;
            } else {
                r[i] = st.top() + 1;
            }
            st.push(i);
        }

        val = 0;
        for (size_t i = 0; i < n; i++) {
            val = max(val, 1LL * (r[i] - l[i] - 1) * v[i]);
        }
        return val;
    }
};

class Fadema 
{
public:
    // https://www.infoarena.ro/job_detail/3349666
    static int solve(vector<vector<int>> a)
    {
        int n = a.size();
        int m = a[0].size();
        int mx = 1;
        vector<vector<int>> v(n, vector<int>(m, 0));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (v[i][j] != 0) continue;
                v[i][j] = 1;
                int y = i + 1;
                while (y < n && a[y - 1][j] != a[y][j]) {
                    v[y][j] = v[y - 1][j] + 1;
                    y++;
                } 
            }
        }

        for (int i = 0; i < n; i++) {
            vector <int> aux;
            for (int j = 0; j < m; j++) {
                if (j > 0 && a[i][j] == a[i][j - 1]) {
                    mx = max(mx, (int)Strabunica::solve(aux));
                    aux.clear();
                }
                aux.push_back(v[i][j]);
            }
            mx = max(mx, (int)Strabunica::solve(aux));
        }

        return mx;
    }
};

class Alee
{
public:
    /// https://infoarena.ro/job_detail/3354673
    static int isIn(pair<int,int> s, pair<int,int>x) 
    {
        if (x.first <= 0 || x.second <= 0) return 0;
        if (x.first > s.first || x.second > s.second) return 0;
        return 1;
    }

    static int solve(int n, vector<pair<int,int>> &v, pair<int,int> x1, pair<int,int> x2)
    {
        vector<vector<int>> a(n + 1, vector<int>(n + 1, 0));
        vector<vector<int>> d(n + 1, vector<int>(n + 1, 0x7fffffff));
        
        int dx[] = {-1, 1, 0, 0};
        int dy[] = {0, 0, -1, 1};
        
        for (auto w : v) {
            a[w.first][w.second] = 1;
        }
        queue<pair<int,int>> q;
        q.push(x1);
        d[x1.first][x1.second] = 1;
        while (!q.empty()) {
            auto k = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                if (isIn({n, n}, {k.first + dx[i], k.second + dy[i]}) && 
                    d[k.first + dx[i]][k.second + dy[i]] > d[k.first][k.second] + 1 &&
                    a[k.first + dx[i]][k.second + dy[i]] != 1) {
                    d[k.first + dx[i]][k.second + dy[i]] = d[k.first][k.second] + 1;
                    q.push({k.first + dx[i], k.second + dy[i]});
                }
            }
        }
        return d[x2.first][x2.second];
    }
};

class Vase
{
public:
    static void solve(int a, int b, int c) 
    {

    }
};

class Deque
{
public:
    static long long solve(vector<int> &a, int k) 
    {
        deque<int> q;
        long long S = 0;
        for (int i = 0; i < a.size(); i++) {
            int x = a[i];
            while (!q.empty() && q.front() <= i - k) {
                q.pop_front();
            } 
            while (!q.empty() && a[q.back()] >= x) {
                q.pop_back();
            }
            q.push_back(i);
            if (i >= k - 1) {
                S += 1LL * a[q.front()];
            }
        }
        return S;
    }
};

int main()
{
    ifstream fin("deque.in");
    ofstream fout("deque.out");
    int n, k;
    fin >> n >> k;
    vector<int> a(n, 0);
    for (int i = 0; i < n; i++) {
        fin >> a[i];
    }
    fout << Deque::solve(a, k) << "\n";
    return 0;
}