1090 - Ferris Wheel

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int n, x, l = 0, ans = 0;
    cin >> n >> x;
    vi p(n);
    for (auto &pi : p)
        cin >> pi;
    sort(p.begin(), p.end());
    for (int i = n - 1; i >= l; --i)
    {
        if (p[i] + p[l] <= x)
            ++l;
        ++ans;
    }
    cout << ans << endl;
    return 0;
}


1085D - Minimum Diameter Tree

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 1e5 + 11;
int n, s, deg[NMAX];
vi g[NMAX];
void dfs(int u, int p)
{
    deg[u] = 0;
    for (auto v : g[u])
    {
        deg[u]++;
        if (v != p)
            dfs(v, u);
    }
}
int main(void)
{
    cin >> n >> s;
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    int l = count_if(deg + 1, deg + n + 1, [](int d) { return d == 1; });
    cout << fixed << setprecision(12) << (2.0 * s) / (1.0 * l) << endl;
    return 0;
}


1085 - Array Division

Click to show code.


using namespace std;
using ll = long long;
using predicate = function<bool(ll)>;
const int NMAX = 2e5 + 11;
ll n, k, x[NMAX];
ll bs(ll l, ll r, predicate p)
{
    while (l < r)
    {
        ll m = l + (r - l) / 2;
        if (p(m))
            r = m;
        else
            l = m + 1;
    }
    return l;
}
bool max_sum_test(ll max_sum)
{
    ll last_sum = 0;
    int n_partitions = 0;
    for (int i = 0; i < n; ++i)
    {
        if (last_sum + x[i] > max_sum)
        {
            last_sum = 0;
            n_partitions += 1;
        }
        last_sum += x[i];
    }
    n_partitions += 1;
    return (n_partitions <= k);
}
int main(void)
{
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
        cin >> x[i];
    cout << bs(*max_element(x, x + n), 1e18, max_sum_test) << endl;
    return 0;
}


1084 - Apartments

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int n, m, k, ix, ans;
    cin >> n >> m >> k;
    vi a(n), b(m);
    for (auto &ai : a)
        cin >> ai;
    for (auto &bi : b)
        cin >> bi;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    ix = 0, ans = 0;
    for (auto bi : b)
    {
        while (ix < n and a[ix] < bi - k)
            ++ix;
        if (bi - k <= a[ix] and a[ix] <= bi + k)
            ++ix, ++ans;
        if (ix >= n)
            break;
    }
    cout << ans << endl;
    return 0;
}


1083 - Missing Number

Click to show code.


using namespace std;
int main(void)
{
    int n, x;
    cin >> n;
    vector<bool> is_present(n + 1, false);
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> x;
        is_present[x] = true;
    }
    for (int i = 1; i <= n; ++i)
        if (not is_present[i])
            cout << i << endl;
    return 0;
}


1082 - Sum Of Divisors

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));
}
template <long long M, typename T = long long>
class NumMod
{
    static_assert(std::is_integral<T>::value, "Integral required.");
    T x;
  public:
    NumMod<M, T>(T x) : x(x) {}
    NumMod<M, T>(NumMod<M, T> &n) : x(n.x) {}
    explicit operator T() const { return x; }
    T v(void) const { return (this->x + M) % M; }
    NumMod<M, T> &operator=(NumMod<M, T> const &y)
    {
        this->x = y.v();
        return *this;
    }
    NumMod<M, T> &operator=(T const &y)
    {
        return this->operator=(NumMod<M, T>(y));
    }
    NumMod<M, T> operator+(NumMod<M, T> const &y) { return (v() + y.v()) % M; }
    NumMod<M, T> operator+(T const &y)
    {
        return this->operator+(NumMod<M, T>(y));
    }
    NumMod<M, T> operator-(NumMod<M, T> const &y) { return (v() - y.v()) % M; }
    NumMod<M, T> operator-(T const &y)
    {
        return this->operator-(NumMod<M, T>(y));
    }
    NumMod<M, T> &operator+=(const NumMod<M, T> &y)
    {
        return this->operator=(operator+(y));
    }
    NumMod<M, T> operator*(NumMod<M, T> y) { return (v() * y.v()) % M; }
    NumMod<M, T> operator*(T y) { return this->operator*(NumMod<M, T>(y)); }
};
ll const MOD = 1e9 + 7;
NumMod<MOD> sum(NumMod<MOD> n) { return n * (n + 1) * ((MOD + 1) / 2); }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    ll n;
    cin >> n;
    NumMod<MOD> ans(0);
    for (ll i = 1; i <= n; ++i)
    {
        ll times = n / i, r = n / times;
        ans += NumMod<MOD>(times) * (sum(r) - sum(i - 1));
        i = r;
    }
    cout << ll(ans) << endl;
    return 0;
}


1081 - Common Divisors

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 n;
    cin >> n;
    int const NMAX = 1e6;
    vi freq(NMAX + 1, 0);
    for (int i = 0; i < n; ++i)
    {
        int x;
        cin >> x;
        freq[x]++;
    }
    for (int i = NMAX; i > 0; --i)
    {
        ll cnt = 0;
        for (int j = i; j <= NMAX; j += i)
            cnt += freq[j];
        if (cnt >= 2)
        {
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}


1079 - Binomial Coefficients

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));
}
template <long long M, typename T = long long>
class NumMod
{
    static_assert(std::is_integral<T>::value, "Integral required.");
    T x;
  public:
    NumMod<M, T>(T x) : x(x) {}
    NumMod<M, T>() : x(0) {}
    NumMod<M, T>(NumMod<M, T> &n) : x(n.x) {}
    explicit operator T() const { return x; }
    T v(void) const { return (this->x + M) % M; }
    NumMod<M, T> &operator=(NumMod<M, T> const &y)
    {
        this->x = y.v();
        return *this;
    }
    NumMod<M, T> &operator=(T const &y)
    {
        return this->operator=(NumMod<M, T>(y));
    }
    NumMod<M, T> operator+(NumMod<M, T> y) const { return (v() + y.v()) % M; }
    NumMod<M, T> operator+(T y) const
    {
        return this->operator+(NumMod<M, T>(y));
    }
    NumMod<M, T> operator-(NumMod<M, T> y) const { return (v() - y.v()) % M; }
    NumMod<M, T> operator-(T y) const
    {
        return this->operator-(NumMod<M, T>(y));
    }
    NumMod<M, T> &operator+=(NumMod<M, T> y)
    {
        return this->operator=(operator+(y));
    }
    NumMod<M, T> operator*(NumMod<M, T> y) const { return (v() * y.v()) % M; }
    NumMod<M, T> operator*(T y) const
    {
        return this->operator*(NumMod<M, T>(y));
    }
    NumMod<M, T> &operator*=(NumMod<M, T> y)
    {
        return this->operator=(operator*(y));
    }
    NumMod<M, T> operator/(NumMod<M, T> y)
    {
        return this->operator*(inverse(y));
    }
    NumMod<M, T> inverse(NumMod<M, T> y) { return binpow(y, M - 2); }
};
int const NMAX = 1e6 + 11;
int const MOD = 1e9 + 7;
NumMod<MOD, ll> fact[NMAX];
void precompute_fact(void)
{
    fact[0] = 1;
    for (int i = 1; i < NMAX; ++i)
        fact[i] = fact[i - 1] * i;
}
template <typename T>
T binpow(T a, ll b)
{
    T res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = res * a;
        a *= a;
        b >>= 1;
    }
    return res;
}
ll C(ll n, ll k)
{
    if (k > n)
        return 1;
    return ll(fact[n] / (fact[n - k] * fact[k]));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    precompute_fact();
    while (t--)
    {
        ll n, k;
        cin >> n >> k;
        cout << C(n, k) << endl;
    }
    return 0;
}


1076D - Edge Deletion

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 3e5 + 11;
ll const INF = 1e17;
int n, m, k;
vector<ii> g[NMAX];
array<int, 3> edges[NMAX];
void dfs(int u, int parent, int &rem, vector<vi> &spt, vi &p)
{
    p[u] = parent;
    for (auto v : spt[u])
        if (rem > 0)
            dfs(v, u, --rem, spt, p);
}
vi dijkstra(int s)
{
    vector<ll> d(n + 1, INF);
    vi p(n + 1, -1);
    set<pair<ll, int>> q;
    d[s] = 0;
    q.insert({0, s});
    while (!q.empty())
    {
        int u = q.begin()->second;
        q.erase(q.begin());
        for (auto [v, w] : g[u])
        {
            if (d[u] + w < d[v])
            {
                q.erase({d[v], v});
                d[v] = d[u] + w;
                p[v] = u;
                q.insert({d[v], v});
            }
        }
    }
    return p;
}
int main(void)
{
    int u, v, w;
    cin >> n >> m >> k;
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
        edges[i] = {u, v, w};
    }
    auto p = dijkstra(1);
    if (k < n - 1)
    {
        vector<vi> spt(n + 1);
        for (int v = 1; v <= n; ++v)
        {
            u = p[v];
            if (u != -1)
                spt[u].push_back(v);
        }
        fill(p.begin(), p.end(), -1);
        int rem = k;
        dfs(1, -1, rem, spt, p);
    }
    cout << min(k, n - 1) << endl;
    for (int i = 0; i < m; ++i)
    {
        auto [u, v, w] = edges[i];
        if (p[u] == v or p[v] == u)
            cout << i + 1 << " ";
    }
    return 0;
}


1076 - Sliding Median

Click to show code.


using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using ordered_set = tree<ii,
                         null_type,
                         less<ii>,
                         rb_tree_tag,
                         tree_order_statistics_node_update>;
int n, k;
vector<int> a;
ordered_set s;
void build(void)
{
    for (int i = 0; i < k; ++i)
        s.insert({a[i], i});
}
int query(void) { return s.find_by_order((k - 1) / 2)->first; }
void increment(int l)
{
    s.erase(s.find({a[l], l}));
    s.insert({a[l + k], l + k});
}
int main(void)
{
    cin >> n >> k;
    a.resize(n);
    for (auto &ai : a)
        cin >> ai;
    build();
    for (int l = 0; l < n - k + 1; ++l)
    {
        cout << query() << " ";
        if (l + k != n)
            increment(l);
    }
    return 0;
}