1478A - Nezzar And Colorful Balls

Note that each subarray of equal value has to be painted with $x$ colors, where $x$ is the size of such subarray. This is because otherwise, this would yield a color sequence that is not strictly increasing.

To find the minimum number of colors needed, we need to find the biggest subarray of equal value.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi freq(n + 1, 0);
        for (int i = 0; i < n; ++i)
        {
            int ai;
            cin >> ai;
            freq[ai]++;
        }
        cout << *max_element(begin(freq), end(freq)) << endl;
    }
    return 0;
}


145E - Lucky Queries

Click to show code.


using namespace std;
struct node
{
    int n4, n7;
    int n47, n74;
};
const int NMAX = 1e6 + 11;
int n, a[NMAX];
bool lazy[4 * NMAX];
node seg[4 * NMAX];
node merge(node u, node v)
{
    node ans;
    ans.n4 = u.n4 + v.n4;
    ans.n7 = u.n7 + v.n7;
    ans.n47 = max({u.n4 + v.n47, u.n47 + v.n7});
    ans.n74 = max({u.n7 + v.n74, u.n74 + v.n4});
    return ans;
}
inline node make_leaf(int d) { return {(d == 4), (d == 7), 1, 1}; }
void lazy_propagate(int v, int tl, int tr)
{
    swap(seg[v].n4, seg[v].n7);
    swap(seg[v].n47, seg[v].n74);
    if (tl != tr)
    {
        lazy[2 * v] = !lazy[2 * v];
        lazy[2 * v + 1] = !lazy[2 * v + 1];
    }
    lazy[v] = !lazy[v];
}
void build(int a[], int v, int tl, int tr)
{
    if (tl == tr)
        seg[v] = make_leaf(a[tl]);
    else
    {
        int tm = (tl + tr) / 2;
        build(a, 2 * v, tl, tm);
        build(a, 2 * v + 1, tm + 1, tr);
        seg[v] = merge(seg[2 * v], seg[2 * v + 1]);
    }
}
void update(int v, int tl, int tr, int ql, int qr)
{
    if (lazy[v])
        lazy_propagate(v, tl, tr);
    if (ql > qr)
        return;
    if (tl == ql and tr == qr)
    {
        lazy_propagate(v, tl, tr);
        lazy[v] = false;
    }
    else
    {
        int tm = (tl + tr) / 2;
        update(2 * v, tl, tm, ql, min(tm, qr));
        update(2 * v + 1, tm + 1, tr, max(tm + 1, ql), qr);
        seg[v] = merge(seg[2 * v], seg[2 * v + 1]);
    }
}
node query(int v, int tl, int tr, int ql, int qr)
{
    if (lazy[v])
        lazy_propagate(v, tl, tr);
    if (ql > qr)
        return {};
    if (tl == ql and tr == qr)
        return seg[v];
    else
    {
        int tm = (tl + tr) / 2;
        return merge(query(2 * v, tl, tm, ql, min(tm, qr)),
                     query(2 * v + 1, tm + 1, tr, max(tm + 1, ql), qr));
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int q, l, r;
    string s, type;
    cin >> n >> q;
    cin >> s;
    for (int i = 0; i < n; ++i)
        a[i] = s[i] - '0';
    build(a, 1, 0, n - 1);
    while (q--)
    {
        cin >> type;
        if (type == "count")
        {
            node ans = query(1, 0, n - 1, 0, n - 1);
            cout << ans.n47 << endl;
        }
        else
        {
            cin >> l >> r;
            update(1, 0, n - 1, l - 1, r - 1);
        }
    }
    return 0;
}


1420D - Rescue Nibel

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<ll>;
ll const MOD = 998244353;
int const NMAX = 3e5 + 11;
ll fact[NMAX];
void precompute()
{
    fact[0] = 1;
    for (int i = 1; i < NMAX; i++)
        fact[i] = fact[i - 1] * i % MOD;
}
ll gcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}
ll inv(int a)
{
    ll x, y;
    ll g = gcd(a, MOD, x, y);
    if (g != 1)
    {
        return 0;
    }
    else
    {
        return (x % MOD + MOD) % MOD;
    }
}
ll C(ll n, ll k)
{
    return fact[n] * inv(fact[k] * fact[n - k] % MOD) % MOD;
}
int main(void)
{
    ll n, k;
    cin >> n >> k;
    vector<ii> events(2 * n);
    for (int i = 0; i < n; ++i)
    {
        ll l, r;
        cin >> l >> r;
        events[2 * i] = {l, -1};
        events[2 * i + 1] = {r, +1};
    }
    sort(events.begin(), events.end());
    precompute();
    ll turned = 0;
    ll ans = 0;
    for (auto [t, sign] : events)
    {
        sign *= -1;
        if (sign == +1 and turned + sign >= k)
        {
            ans += (C(turned, k - 1)) % MOD;
        }
        turned += sign;
    }
    cout << ans % MOD << endl;
    return 0;
}


1420C1 - Pokemon Army

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
inline bool maxima(int a, int b, int c) { return a <= b and b >= c; }
inline bool minima(int a, int b, int c) { return a >= b and b <= c; }
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, q;
        cin >> n >> q;
        vi a(n + 2, 0);
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        bool increasing = true;
        ll ans = 0;
        for (int i = 1; i <= n; ++i)
        {
            if (maxima(a[i - 1], a[i], a[i + 1]) and increasing)
            {
                ans += a[i];
                increasing = false;
            }
            else if (minima(a[i - 1], a[i], a[i + 1]) and not increasing)
            {
                ans -= a[i];
                increasing = true;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


1420B - Rock And Lever

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<ll>;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n), cnt(64, 0);
        for (auto &ai : a)
        {
            cin >> ai;
            cnt[__builtin_clz(ai)]++;
        }
        ll ans = 0;
        for (auto x : cnt)
        {
            if (x == 0 or x == 1)
                continue;
            ans += (x * (x - 1)) / 2;
        }
        cout << ans << endl;
    }
    return 0;
}


1420A - Cubes Sorting

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n);
        for (auto &ai : a)
            cin >> ai;
        bool ans = is_sorted(a.begin(), a.end(), [](int a, int b) { return a >= b; });
        cout << (ans ? "NO" : "YES") << endl;
    }
    return 0;
}


1419D1 - Sage Birthday

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int n;
    cin >> n;
    vi a(n);
    for (auto &ai : a)
        cin >> ai;
    sort(a.begin(), a.end(), greater<int>());
    vi ans(n);
    ans[0] = a[0];
    for (int i = 1; i < n; i += 2)
    {
        ans[i] = a[i];
        if (i + 1 < n)
        {
            ans[i + 1] = a[i + 1];
            swap(ans[i], ans[i + 1]);
        }
    }
    cout << (n - 1) / 2 << endl;
    for (auto x : ans)
        cout << x << " ";
    cout << endl;
    return 0;
}


1419B - Stairs

Click to show code.


using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
using predicate = function<bool(ll)>;
ll x;
ll sq(ll n) { return (n * (n + 1)) / 2; }
int bs(ll l, ll r, predicate p)
{
    while (l < r)
    {
        ll m = l + (r - l + 1) / 2;
        if (p(m))
            l = m;
        else
            r = m - 1;
    }
    return l;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> x;
        ll y = 2, ans = 0;
        while (sq(y - 1) <= x)
        {
            ans += 1;
            x -= sq(y - 1);
            y <<= 1;
        }
        cout << ans << endl;
    }
    return 0;
}


1418A - Buying Torches

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
inline ll ceil(ll a, ll b) { return (a + b - 1) / b; }
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll x, y, k;
        cin >> x >> y >> k;
        ll op1 = ceil(k * (y + 1) - 1, x - 1);
        ll op2 = k;
        cout << op1 + op2 << endl;
    }
    return 0;
}


1417C - K Amazing Numbers

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n), ans(n, -1), last(n + 1, -1), sep(n + 1, 0);
        read_n(a.begin(), n);
        for (int i = 0; i < n; ++i)
        {
            sep[a[i]] = max(sep[a[i]], i - last[a[i]]);
            last[a[i]] = i;
        }
        for (int x = 1; x <= n; ++x)
            sep[x] = max(sep[x], n - last[x]);
        for (int x = 1; x <= n; ++x)
        {
            int first_k = sep[x];
            if (first_k == n + 1 or ans[first_k - 1] != -1)
                continue;
            ans[first_k - 1] = x;
        }
        int prv = -1;
        for (int i = 0; i < n; ++i)
        {
            if (prv != -1)
            {
                if (ans[i] == -1 or ans[i] > prv)
                    ans[i] = prv;
            }
            prv = ans[i];
        }
        write(ans.begin(), ans.end(), " "), cout << endl;
    }
    return 0;
}