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


1409D - Decrease Sum Digits

Click to show code.


using namespace std;
using ll = long long;
int dsum(ll n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
ll make_zero(ll n, int i)
{
    int j = i;
    while (i--)
    {
        n /= 10;
    }
    n += 1;
    while (j--)
    {
        n *= 10;
    }
    return n;
}
ll solve(ll n, ll s)
{
    ll ans = 0, i = 1;
    while (dsum(n) > s)
    {
        ll cur = make_zero(n, i);
        ans += (cur - n);
        n = cur;
        ++i;
    }
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        cout << solve(n, s) << endl;
    }
    return 0;
}


1409C - Yet Another Array Restoration

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, x, y;
        cin >> n >> x >> y;
        ll bmax = 1e9 + 7, bdiff;
        for (int between = 0; between <= n - 2; ++between)
        {
            if ((y - x) % (between + 1) != 0)
                continue;
            ll diff = (y - x) / (between + 1);
            ll cmin = x, cnt = n - 2 - between;
            while ((cmin - diff) >= 1 and cnt > 0)
            {
                cnt--;
                cmin -= diff;
            }
            ll cmax = cmin + (n - 1) * diff;
            if (cmax < bmax)
            {
                bmax = cmax;
                bdiff = diff;
            }
        }
        for (int i = 0; i < n; ++i)
            cout << bmax - bdiff * i << " ";
        cout << endl;
    }
    return 0;
}


1409B - Minimum Product

Click to show code.


using namespace std;
using ll = long long;
pair<ll, ll> op(ll a, ll x, ll n)
{
    ll p = max(a - n, x);
    n = n - (a - p);
    return {p, n};
}
ll simulate(ll a, ll b, ll x, ll y, ll n)
{
    auto [ap, np1] = op(a, x, n);
    a = ap, n = np1;
    auto [bp, np2] = op(b, y, n);
    b = bp, n = np2;
    return a * b;
}
int main(void)
{
    ll t;
    cin >> t;
    while (t--)
    {
        ll a, b, x, y, n;
        cin >> a >> b >> x >> y >> n;
        ll ans0 = simulate(a, b, x, y, n);
        ll ans1 = simulate(b, a, y, x, n);
        cout << min(ans0, ans1) << endl;
    }
    return 0;
}


1409A - Yet Another Two Integers

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll a, b;
        cin >> a >> b;
        ll diff = abs(b - a);
        ll ans = diff / 10 + ((diff % 10) > 0);
        cout << ans << endl;
    }
    return 0;
}


1408C - Discrete Acceleration

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using predicate = function<bool(double)>;
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));
}
ll n, L;
vi a, ar;
double T(double d, vi const &flag)
{
    int i = 0, flag_prev = 0;
    double tans = 0.0;
    double cur_speed = 1.0;
    while (i < n and flag[i] < d)
    {
        tans += (flag[i] - flag_prev) / cur_speed;
        flag_prev = flag[i];
        cur_speed++;
        ++i;
    }
    tans += (d - flag_prev) / cur_speed;
    return tans;
}
double bs(double l, double r, predicate p)
{
    for (int iter = 0; iter < 1000; ++iter)
    {
        double m = l + (r - l) / 2.0;
        if (p(m))
            r = m;
        else
            l = m;
    }
    return T(l, a);
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> L;
        a.resize(n), ar.resize(n);
        read_n(a.begin(), n);
        for (int i = 0; i < n; ++i)
            ar[n - i - 1] = L - a[i];
        cout << fixed << setprecision(9) << bs(0.0, double(L), [](double d) {
            return T(d, a) - T(L - d, ar) >= 0.0;
        }) << endl;
    }
    return 0;
}


1408B - Arrays Sum

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)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vi a(n);
        read_n(a.begin(), n);
        int nu = distance(a.begin(), unique(a.begin(), a.end()));
        if (k == 1 and nu > 1)
            cout << -1 << endl;
        else
        {
            int ans = 0;
            do
            {
                nu -= min(k, nu);
                nu++;
                ans++;
            } while (nu != 1);
            cout << ans << endl;
        }
    }
    return 0;
}


1408A - Circle Coloring

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)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi abc[3], ans(n);
        for (int j = 0; j < 3; ++j)
        {
            abc[j].resize(n);
            read_n(begin(abc[j]), n);
        }
        ans[0] = abc[0][0];
        for (int i = 1; i < n - 1; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                if (abc[j][i] != ans[i - 1])
                {
                    ans[i] = abc[j][i];
                    break;
                }
            }
        }
        for (int j = 0; j < 3; ++j)
        {
            if (abc[j][n - 1] != ans[n - 2] and abc[j][n - 1] != ans[0])
            {
                ans[n - 1] = abc[j][n - 1];
                break;
            }
        }
        write(ans.begin(), ans.end(), " "), cout << endl;
    }
    return 0;
}