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;
}


1417B - Two Arrays

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, T;
        cin >> n >> T;
        vi a(n), ans(n);
        read_n(a.begin(), n);
        int cnt = count_if(a.begin(), a.end(), [T](int ai) { return 2 * ai == T; });
        cnt /= 2;
        for (int i = 0; i < n; ++i)
        {
            if (2 * a[i] < T)
                ans[i] = 0;
            else if (2 * a[i] == T and cnt)
            {
                ans[i] = 0;
                cnt--;
            }
            else
                ans[i] = 1;
        }
        write(ans.begin(), ans.end(), " "), cout << endl;
    }
    return 0;
}


1417A - Copy Paste

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, k;
        cin >> n >> k;
        vi a(n);
        read_n(a.begin(), n);
        sort(a.begin(), a.end());
        int minv = a[0];
        ll ans =
            accumulate(a.begin() + 1, a.end(), 0LL, [minv, k](ll acc, int ai) {
                return acc + (k - ai) / minv;
            });
        cout << ans << endl;
    }
    return 0;
}


1409E - Two Platforms

Click to show code.


using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
int const NMAX = 2e5 + 11;
ll n, k;
ll x[NMAX];
pll nsaved(int i)
{
    auto it = upper_bound(x + i, x + n, x[i] + k);
    return make_pair(distance(x + i, it), distance(x, it));
}
ll solve(void)
{
    vector<pll> saved_at(n);
    vector<ll> best_from(n + 1, 0);
    sort(x, x + n);
    for (int i = n - 1; i >= 0; --i)
    {
        saved_at[i] = nsaved(i);
        best_from[i] = max(saved_at[i].first, best_from[i + 1]);
    }
    ll ans = 0;
    for (int i = 0; i < n; ++i)
    {
        auto [lans, j] = saved_at[i];
        auto rans = best_from[j];
        ans = max(lans + rans, ans);
    }
    return ans;
}
int main(void)
{
    int t, trash;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 0; i < n; ++i)
            cin >> x[i];
        for (int i = 0; i < n; ++i)
            cin >> trash;
        cout << solve() << endl;
    }
    return 0;
}