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


1397A - Juggling Letters

Click to show code.


using namespace std;
using vi = vector<int>;
bool possible(vi cnt, int n)
{
    for (auto freq : cnt)
    {
        if (freq % n != 0)
            return false;
    }
    return true;
}
int main(void)
{
    int t, n;
    string s;
    cin >> t;
    while (t--)
    {
        cin >> n;
        vi cnt(26, 0);
        for (int i = 0; i < n; ++i)
        {
            cin >> s;
            for (auto c : s)
                cnt[c - 'a']++;
        }
        cout << (possible(cnt, n) ? "YES" : "NO") << endl;
    }
    return 0;
}


1395C - Boboniu Bit Operations

Click to show code.


using namespace std;
using vi = vector<int>;
int n, m;
vi a, b;
bool possible(int mask)
{
    for (auto const &ai : a)
    {
        bool ok = false;
        for (auto const &bi : b)
        {
            if (((ai & bi) | mask) == mask)
            {
                ok = true;
                break;
            }
        }
        if (not ok)
            return false;
    }
    return true;
}
int solve(void)
{
    for (int mask = 0; mask < (1 << 9); ++mask)
    {
        if (possible(mask))
            return mask;
    }
    return (1 << 9) - 1;
}
int main(void)
{
    cin >> n >> m;
    a.resize(n);
    b.resize(m);
    for (auto &ai : a)
        cin >> ai;
    for (auto &bi : b)
        cin >> bi;
    cout << solve() << endl;
    return 0;
}


1395A - Boboniu Likes To Color Balls

Click to show code.


using namespace std;
using vi = vector<int>;
bool is_odd(int c) { return c % 2 == 1; }
bool is_even(int c) { return c % 2 == 0; }
bool possible(vi rgbw)
{
    return *min_element(rgbw.begin(), prev(rgbw.end())) >= 1;
}
bool solve(vi rgbw)
{
    if (all_of(rgbw.begin(), rgbw.end(), is_even))
        return true;
    if (rgbw.back() % 2 == 1)
    {
        int cnt = count_if(rgbw.begin(), prev(rgbw.end()), is_odd);
        if (cnt == 0)
            return true;
        else if (cnt == 1)
            return false;
        else
            return possible(rgbw);
    }
    else
    {
        int cnt = count_if(rgbw.begin(), prev(rgbw.end()), is_odd);
        if (cnt == 0)
            return true;
        if (cnt == 1)
            return true;
        else if (cnt == 2)
            return false;
        else
            return possible(rgbw);
    }
}
int main(void)
{
    int t;
    vi rgbw(4);
    cin >> t;
    while (t--)
    {
        for (auto &c : rgbw)
            cin >> c;
        cout << (solve(rgbw) ? "Yes" : "No") << endl;
    }
    return 0;
}