1401A - Distance Axis

Click to show code.


using namespace std;
int solve(int n, int k)
{
    if (n < k)
        return k - n;
    else
        return n % 2 != k % 2;
}
int main(void)
{
    int t, n, k;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        cout << solve(n, k) << endl;
    }
    return 0;
}


1399D - Binary String To Subsequences

Click to show code.


using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
int main(void)
{
    int t, n;
    string s;
    vi ans;
    set<ii> frontier;
    cin >> t;
    while (t--)
    {
        cin >> n >> s;
        ans.assign(n, -1);
        for (int i = 0; i < n; ++i)
        {
            auto it = frontier.lower_bound({'1' - s[i], 0});
            if (it != frontier.end() and it->first != (s[i] - '0'))
            {
                auto [t, id] = *it;
                frontier.erase(it);
                ans[i] = id;
                frontier.insert({s[i] - '0', ans[i]});
            }
            else
            {
                ans[i] = frontier.size();
                frontier.insert({s[i] - '0', ans[i]});
            }
        }
        cout << frontier.size() << endl;
        for (int i = 0; i < n; ++i)
            cout << ans[i] + 1 << " ";
        cout << endl;
        frontier.clear();
    }
    return 0;
}


1399C - Boats Competition

Click to show code.


using namespace std;
using vi = vector<int>;
int const NMAX = 55;
int solve(int n, vi w)
{
    map<int, int> sum_count;
    vi freq(NMAX, 0);
    for (int i = 0; i < n; ++i)
        freq[w[i]]++;
    for (int i = 1; i < NMAX; ++i)
    {
        for (int j = i; j < NMAX; ++j)
        {
            if (i == j)
                sum_count[i + j] += freq[i] / 2;
            else
                sum_count[i + j] += min(freq[i], freq[j]);
        }
    }
    return max_element(sum_count.begin(),
                       sum_count.end(),
                       [](const auto &p1, const auto &p2) {
                           return p1.second < p2.second;
                       })
        ->second;
}
int main(void)
{
    int t, n;
    vi w;
    cin >> t;
    while (t--)
    {
        cin >> n;
        w.resize(n);
        for (auto &wi : w)
            cin >> wi;
        cout << solve(n, w) << endl;
    }
    return 0;
}


1399B - Gifts Fixing

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
ll solve(int n, const vi &a, const vi &b)
{
    ll ans = 0;
    int a0 = *min_element(a.begin(), a.end());
    int b0 = *min_element(b.begin(), b.end());
    for (int i = 0; i < n; ++i)
    {
        ans += max(a[i] - a0, b[i] - b0);
    }
    return ans;
}
int main(void)
{
    int t, n;
    vi a, b;
    cin >> t;
    while (t--)
    {
        cin >> n;
        a.resize(n), b.resize(n);
        for (auto &ai : a)
            cin >> ai;
        for (auto &bi : b)
            cin >> bi;
        cout << solve(n, a, b) << endl;
    }
    return 0;
}


1399A - Remove Smallest

Click to show code.


using namespace std;
using vi = vector<int>;
bool solve(int n, vi a)
{
    sort(a.begin(), a.end());
    for (int i = 1; i < n; ++i)
    {
        if (a[i] - a[i - 1] > 1)
            return false;
    }
    return true;
}
int main(void)
{
    int t, n;
    vi a;
    cin >> t;
    while (t--)
    {
        cin >> n;
        a.assign(n, 0);
        for (auto &ai : a)
            cin >> ai;
        cout << (solve(n, a) ? "YES" : "NO") << endl;
    }
    return 0;
}


1398D - Colored Rectangles

Click to show code.


using namespace std;
using ms = multiset<int, greater<int>>;
using vi = vector<int>;
int const NMAX = 200 + 11;
int dp[NMAX][NMAX][NMAX], nrgb[3];
vi rgb[3];
int solve(void)
{
    auto [r, g, b] = rgb;
    auto [nr, ng, nb] = nrgb;
    for (int ir = 0; ir <= nr; ++ir)
    {
        for (int ig = 0; ig <= ng; ++ig)
        {
            for (int ib = 0; ib <= nb; ++ib)
            {
                int &ans = dp[ir][ig][ib];
                if (ig > 0 and ib > 0)
                    ans = dp[ir][ig - 1][ib - 1] + g[ig] * b[ib];
                if (ir > 0 and ig > 0)
                    ans = max(ans, dp[ir - 1][ig - 1][ib] + r[ir] * g[ig]);
                if (ir > 0 and ib > 0)
                    ans = max(ans, dp[ir - 1][ig][ib - 1] + r[ir] * b[ib]);
            }
        }
    }
    return dp[nr][ng][nb];
}
int main(void)
{
    cin >> nrgb[0] >> nrgb[1] >> nrgb[2];
    for (int i = 0; i < 3; ++i)
    {
        rgb[i].resize(nrgb[i] + 1);
        for (int j = 1; j <= nrgb[i]; ++j)
            cin >> rgb[i][j];
        sort(next(rgb[i].begin()), rgb[i].end());
    }
    cout << solve() << endl;
    return 0;
}


1398C - Good Subarrays

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
ll solve(vi a)
{
    int n = (int)a.size();
    ll ans = 0;
    map<ll, int> sum_cnt;
    sum_cnt[0] = 1;
    ll ps = 0;
    for (int i = 0; i < n; ++i)
    {
        ps += a[i];
        ans += sum_cnt[ps];
        ++sum_cnt[ps];
    }
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        string s;
        cin >> n >> s;
        vi a(n);
        transform(s.begin(), s.end(), a.begin(), [](char c) { return c - '0' - 1; });
        cout << solve(a) << endl;
    }
    return 0;
}


1398B - Substring Removal.Game

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int t, cnt;
    string s;
    vi scores;
    cin >> t;
    while (t--)
    {
        cin >> s;
        cnt = 0;
        scores.clear();
        for (auto c : s)
        {
            if (c == '1')
            {
                ++cnt;
            }
            else
            {
                if (cnt != 0)
                    scores.push_back(cnt);
                cnt = 0;
            }
        }
        if (cnt != 0)
            scores.push_back(cnt);
        sort(scores.begin(), scores.end(), greater<int>());
        int ans = 0;
        for (int i = 0, n = scores.size(); i < n; i += 2)
            ans += scores[i];
        cout << ans << endl;
    }
    return 0;
}


1398A - Bad Triangle

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int t, n;
    vi a;
    cin >> t;
    while (t--)
    {
        cin >> n;
        a.resize(n);
        for (auto &ai : a)
            cin >> ai;
        int x = a[0] + a[1];
        auto it = lower_bound(a.begin(), a.end(), x);
        if (it == a.end())
            cout << -1 << endl;
        else
        {
            cout << 1 << " " << 2 << " " << distance(a.begin(), it) + 1 << endl;
        }
    }
    return 0;
}


1397B - Power Sequence

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
using predicate = function<bool(int)>;
int const NMAX = 1e5 + 11;
int n, a[NMAX];
ll cost(int c)
{
    ll ans = 0;
    ll x = 1;
    for (int i = 0; i < n; ++i)
    {
        ans += abs((ll)a[i] - x);
        x *= (ll)c;
    }
    return ans;
}
bool worthit(ll last, int c)
{
    ll ans = 0;
    ll x = 1;
    for (int i = 0; i < n; ++i)
    {
        ans += abs((ll)a[i] - x);
        if (ans > last)
            return false;
        x *= (ll)c;
    }
    return true;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a, a + n);
    ll last = cost(1);
    int c = 2;
    while (worthit(last, c))
    {
        last = cost(c);
        ++c;
    }
    cout << last << endl;
    return 0;
}