Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define ll long long
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;
using namespace std;
//using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> adjList;
typedef pair<int, pair<int, int>> iii;
typedef vector<unsigned int> vui;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vi vm;
typedef vector<vm> Mat;



int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#else
    //    freopen("outofplace.in", "r", stdin);
    //    freopen("outofplace.out", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed;

    int n; cin >> n;
    string s; cin >> s;
    vector<vi> pos(2);
    for (int i = 0; i < n; ++i) {
        pos[s[i] - '0'].push_back(i);
    }
    int ans = 0;
    for (int i = 0; i < pos[s[0] - '0'].size(); ++i) {
        int j = *lower_bound(all(pos[s.back() - '0']), pos[s[0] - '0'][i]);
        ans = max(ans, pos[s[0] - '0'][i] + n - j - 1);
    }

    cout << ans << '\n';

    return 0;
}
Copy
Pop Pop Luka
GNU G++17
3 ms
1.4 MB
Accepted