PYRAMID2 - Duyệt binh

Tác giả: ladpro98

Ngôn ngữ: C++

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("PYRAMID2.txt", "r", stdin);
#endif // ONLINE_JUDGE
    int n; string s;
    cin >> n >> s;
    if (*s.rbegin() == '<') ++n, s += '>';
    vector<int> pos;
    int last = -1;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '>')
            pos.push_back(i);
        else
            last = i;
    }
    while (!pos.empty() && pos.back() > last + 1) pos.pop_back();
    int ans = 0;
    for (int i = int(pos.size()) - 2, cnt = 0, lastDelay = 0; i >= 0; --i) {
        cnt += pos[i + 1] - pos[i] - 1;
        int delay = max(lastDelay - (pos[i + 1] - pos[i]) + 2, 0);
        ans = max(ans, delay + cnt);
        lastDelay = delay;
    }
    cout << ans << endl;
    return 0;
}

Download