Suggestions logoSuggestions

Answers

Detailed solutions and code implementations for Competitive Programming II.

1. 19A - World Football Cup

#include <bits/stdc++.h>
using namespace std;

const int N = 55;

void solve()
{
          int n;
          cin >> n;

          string s[N], x;
          int p[N] = {}, d[N] = {}, g[N] = {}, id[N];

          for (int i = 0; i < n; i++)
          {
                    cin >> s[i];
                    id[i] = i;
          }

          int k = n * (n - 1) / 2;
          while (k--)
          {
                    int a, b, u = 0, v = 0, z;
                    char c;
                    cin >> x >> a >> c >> b;

                    z = x.find('-');
                    while (s[u] != x.substr(0, z))
                              u++;
                    while (s[v] != x.substr(z + 1))
                              v++;

                    g[u] += a;
                    g[v] += b;
                    d[u] += a - b;
                    d[v] += b - a;

                    if (a == b)
                    {
                              p[u]++;
                              p[v]++;
                    }
                    else
                    {
                              if (a > b)
                                        p[u] += 3;
                              else
                                        p[v] += 3;
                    }
          }

          sort(id, id + n, [&](int a, int b)
               { return p[a] != p[b] ? p[a] > p[b] : d[a] != d[b] ? d[a] > d[b]
                                                                  : g[a] > g[b]; });

          vector<string> ans;
          for (int i = 0; i < n / 2; i++)
                    ans.push_back(s[id[i]]);
          sort(ans.begin(), ans.end());
          for (auto x : ans)
                    cout << x << '\n';
}

int main()
{
          ios::sync_with_stdio(false);
          cin.tie(nullptr);

          solve();

          return 0;
}

2. 235B - Let's Play Osu!

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    double ans = 0;
    double cur = 0;

    while (n--)
    {
        double p;
        cin >> p;

        ans += p * (2 * cur + 1);
        cur = p * (cur + 1);
    }

    cout << fixed << setprecision(15) << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

3. Student Management Portal

#include <bits/stdc++.h>
using namespace std;

vector<int> ids;

void show_ids()
{
    for (int x : ids)
        cout << x << ' ';
    cout << '\n';
}

void solve()
{
    string op;
    cin >> op;

    transform(op.begin(), op.end(), op.begin(), ::tolower);

    int id;

    if (op == "register")
    {
        cin >> id;
        ids.push_back(id);
        show_ids();
    }
    else if (op == "search")
    {
        cin >> id;
        auto it = find(ids.begin(), ids.end(), id);

        if (it != ids.end())
            cout << "FOUND" << '\n';
        else
            cout << "NOT FOUND" << '\n';
        show_ids();
    }
    if (op == "remove")
    {
        string s;
        cin >> s;

        sort(ids.begin(), ids.end());
        auto uv = unique(ids.begin(), ids.end());
        ids.erase(uv, ids.end());

        show_ids();
    }
    else if (op == "delete")
    {
        cin >> id;

        auto it = find(ids.begin(), ids.end(), id);
        if (it != ids.end())
            ids.erase(it);

        show_ids();
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

4. CPU Task Scheduler

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    vector<int> id(n), pq(n), ex(n);
    int time = 0;

    for (int i = 0; i < n; i++)
        cin >> id[i] >> pq[i] >> ex[i];

    cout << "Execution Order: " << '\n';
    while (n--)
    {
        auto mx = max_element(pq.begin(), pq.end());
        auto idx = distance(pq.begin(), mx);
        time += ex[idx];
        cout << "Task " << id[idx] << " completes at " << time << '\n';
        pq[idx] = 0;
    }
    cout << "Total time = " << time << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

5. Minimum Swaps to Sort Employee IDs

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    vector<int> a(n);
    for (int &x : a)
        cin >> x;

    long long cnt = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                swap(a[j], a[j + 1]);
                cnt++;
                for (int x : a)
                    cout << x << ' ';
                cout << '\n';
            }
        }
    }
    cout << cnt << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

6. Robot Navigation

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    string s;
    while (cin >> s)
    {
        int x = 0, y = 0;
        set<pair<int, int>> visited;
        visited.insert({x, y});

        bool repeated = false;

        for (char c : s)
        {
            if (c == 'L')
                x--;
            else if (c == 'R')
                x++;
            else if (c == 'U')
                y++;
            else if (c == 'D')
                y--;

            if (visited.count({x, y}))
                repeated = true;

            visited.insert({x, y});
        }

        cout << "(" << x << ", " << y << ")\n";
        cout << (repeated ? "Yes" : "No") << '\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

On this page