1140C - Playlist

Click to show code.


using namespace std;
using ll = long long;
struct Song {
  ll length;
  ll beauty;
  bool operator<(const Song &s) const { return (beauty < s.beauty); }
};
int n, k;
Song songs[300100];
ll max_pleasure(void) {
  ll ans = 0, cur = 0, len = 0;
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  sort(songs, songs + n);
  for (int i = n - 1; i >= 0; --i) {
    len += songs[i].length;
    if (pq.size() == k) {
      len -= pq.top();
      pq.pop();
    }
    pq.push(songs[i].length);
    cur = len * songs[i].beauty;
    ans = max(ans, cur);
  }
  return ans;
}
int main(void) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> songs[i].length;
    cin >> songs[i].beauty;
  }
  cout << max_pleasure() << endl;
  return 0;
}


1140 - Projects

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = array<ll, 3>;
using vi = vector<ll>;
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 solve(int n, vector<iii> projects)
{
    sort(projects.begin(), projects.end());
    vi dp(n + 1, 0);
    for (int i = 1; i <= n; ++i)
    {
        auto [ei, si, di] = projects[i];
        dp[i] = dp[i - 1];
        auto it = lower_bound(projects.begin(), projects.end(), iii{si, 0, 0});
        if (it != projects.begin())
        {
            --it;
            int j = distance(projects.begin(), it);
            dp[i] = max(dp[i], dp[j] + di);
        }
    }
    return dp[n];
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n;
    cin >> n;
    vector<iii> projects(n + 1);
    projects[0] = {-1, -1, -1};
    for (int i = 1; i <= n; ++i)
        cin >> projects[i][1] >> projects[i][0] >> projects[i][2];
    cout << solve(n, projects) << endl;
    return 0;
}


1137 - Subtree Queries

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);
}
int const NMAX = 2e5 + 11;
int n, timer, val[NMAX], idval[NMAX], id[NMAX], sz[NMAX];
ll t[4 * NMAX];
vi g[NMAX];
void precompute(int u, int p)
{
    id[u] = timer++;
    sz[u] = 1;
    idval[id[u]] = val[u];
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        precompute(v, u);
        sz[u] += sz[v];
    }
}
void build(int a[], int v, int tl, int tr)
{
    if (tl == tr)
        t[v] = a[tl];
    else
    {
        int tm = (tl + tr) / 2;
        build(a, v * 2, tl, tm);
        build(a, v * 2 + 1, tm + 1, tr);
        t[v] = t[v * 2] + t[v * 2 + 1];
    }
}
ll sum(int v, int tl, int tr, int l, int r)
{
    if (l > r)
        return 0;
    if (l == tl and r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return sum(v * 2, tl, tm, l, min(r, tm)) +
           sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val)
{
    if (tl == tr)
        t[v] = new_val;
    else
    {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v * 2, tl, tm, pos, new_val);
        else
            update(v * 2 + 1, tm + 1, tr, pos, new_val);
        t[v] = t[v * 2] + t[v * 2 + 1];
    }
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int q;
    cin >> n >> q;
    read_n(val, n);
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v, u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    precompute(0, 0);
    build(idval, 1, 0, n - 1);
    while (q--)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int u, x;
            cin >> u >> x, u--;
            update(1, 0, n - 1, id[u], x);
        }
        else
        {
            int u;
            cin >> u, u--;
            cout << sum(1, 0, n - 1, id[u], id[u] + sz[u] - 1) << endl;
        }
    }
    return 0;
}


1136 - Counting Paths

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 const NMAX = 2e5 + 11;
int const LMAX = 20;
int n, timer, val[NMAX], tin[NMAX], tout[NMAX], up[NMAX][LMAX], ans[NMAX];
vi g[NMAX];
void preprocess(int u, int p)
{
    tin[u] = ++timer;
    up[u][0] = p;
    for (int i = 1; i < LMAX; ++i)
        up[u][i] = up[up[u][i - 1]][i - 1];
    for (auto v : g[u])
        if (v != p)
            preprocess(v, u);
    tout[u] = ++timer;
}
void solve(int u, int p)
{
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        solve(v, u);
        val[u] += val[v];
    }
    ans[u] = val[u];
}
bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = LMAX - 1; i >= 0; --i)
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    return up[u][0];
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int m;
    cin >> n >> m;
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int root = 1;
    preprocess(root, root);
    while (m--)
    {
        int u, v, l;
        cin >> u >> v;
        val[u] += 1;
        val[v] += 1;
        l = lca(u, v);
        val[l] -= 1;
        if (l != root)
            val[up[l][0]] -= 1;
    }
    solve(1, 1);
    write(ans + 1, ans + n + 1, " "), cout << endl;
    return 0;
}


1135 - Distance Queries

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 const NMAX = 2e5 + 11;
int const LMAX = 20;
int n, timer, tin[NMAX], tout[NMAX], up[NMAX][LMAX], depth[NMAX];
vi g[NMAX];
void dfs(int u, int p)
{
    tin[u] = ++timer;
    up[u][0] = p;
    for (int i = 1; i < LMAX; ++i)
    {
        up[u][i] = up[up[u][i - 1]][i - 1];
    }
    for (int v : g[u])
    {
        if (v != p)
        {
            depth[v] = depth[u] + 1;
            dfs(v, u);
        }
    }
    tout[u] = ++timer;
}
bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] and tout[u] >= tout[v];
}
int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = LMAX - 1; i >= 0; --i)
    {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int q;
    cin >> n >> q;
    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, 1);
    while (q--)
    {
        int u, v;
        cin >> u >> v;
        cout << depth[u] + depth[v] - 2 * depth[lca(u, v)] << endl;
    }
    return 0;
}


11348 - Exhibition

Click to show code.


using namespace std;
int const AMAX = 1e4 + 11;
int main(void)
{
    int t;
    cin >> t;
    for (int tc = 1; tc <= t; ++tc)
    {
        int n;
        cin >> n;
        vector<set<int>> stamps(n);
        vector<int> cnt(AMAX, 0), ans(n, 0);
        for (int i = 0; i < n; ++i)
        {
            int m, x;
            cin >> m;
            while (m--)
            {
                cin >> x;
                stamps[i].insert(x);
            }
            for (auto x : stamps[i])
                cnt[x]++;
        }
        for (int i = 0; i < n; ++i)
            for (auto x : stamps[i])
                ans[i] += (cnt[x] == 1);
        int total = accumulate(ans.begin(), ans.end(), 0);
        cout << "Case " << tc << ": ";
        for (int i = 0; i < n; ++i)
            cout << fixed << setprecision(6) << (ans[i] * 100.0) / (double)total
                 << "%" << (i < n - 1 ? " " : "");
        cout << endl;
    }
    return 0;
}


11340 - Newspaper

Click to show code.


using namespace std;
inline string format_dollar(int cents)
{
    stringstream ss;
    int dollars = cents / 100;
    cents = cents % 100;
    ss << dollars << "." << setw(2) << setfill('0') << cents << "$";
    return ss.str();
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int k, m;
        map<char, int> value;
        string s;
        cin >> k;
        while (k--)
        {
            char c;
            cin >> c;
            cin >> value[c];
        }
        cin >> m;
        cin.ignore();
        int cents = 0;
        while (m--)
        {
            getline(cin, s);
            cents = accumulate(s.begin(), s.end(), cents, [&value](int acc, char c) {
                return acc + value[c];
            });
        }
        cout << format_dollar(cents) << endl;
    }
    return 0;
}


1133D - Zero Quantity Maximization

Click to show code.


using namespace std;
using lld = long double;
using mdi = map<lld, int>;
const int NMAX = 2 * 10e5 + 11;
int n;
lld a[NMAX], b[NMAX];
mdi counter;
bool comp(const pair<lld, int> &l, const pair<lld, int> &r)
{
    return l.second < r.second;
}
int main(void)
{
    int zeros = 0;
    lld frac;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> b[i];
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == 0 and b[i] == 0)
            ++zeros;
        if (a[i] == 0)
            continue;
        else
        {
            frac = b[i] / a[i];
            ++counter[frac];
        }
    }
    cout << zeros + max_element(counter.begin(), counter.end(), comp)->second
         << endl;
}


1133 - Tree Distances Ii

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 2e5 + 11;
ll n, dp[NMAX], sz[NMAX], ans[NMAX];
vi g[NMAX];
void dfs(int u, int p = -1)
{
    sz[u] = 1;
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        dfs(v, u);
        sz[u] += sz[v];
        dp[u] += dp[v] + sz[v];
    }
}
void reroot(int u, int p = -1)
{
    ans[u] = dp[u];
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        auto temp1 = dp[v], temp2 = sz[v];
        dp[v] = dp[u] + sz[u] - 2 * sz[v];
        sz[v] = sz[u];
        reroot(v, u);
        dp[v] = temp1;
        sz[v] = temp2;
    }
}
int main(void)
{
    int root = 1;
    cin >> n;
    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(root);
    reroot(root);
    for (int i = 1; i <= n; ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}


1132 - Tree Distances I

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 2e5 + 11;
int n, ddown[NMAX], ans[NMAX];
vi g[NMAX];
void dp(int u, int p = -1)
{
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        dp(v, u);
        ddown[u] = max(ddown[u], ddown[v] + 1);
    }
}
void reroot(int u, int p = -1, int dup = 0)
{
    ans[u] = max(ddown[u], dup);
    vector<ii> dists = {{dup, -1}, {0, -1}};
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        dists.emplace_back(ddown[v] + 1, v);
    }
    sort(dists.begin(), dists.end(), greater<ii>());
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        reroot(v,
               u,
               dists[0].second == v ? dists[1].first + 1 : dists[0].first + 1);
    }
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dp(1);
    reroot(1);
    for (int u = 1; u <= n; ++u)
        cout << ans[u] << " ";
    cout << endl;
    return 0;
}