Submission #474341

# Submission time Handle Problem Language Result Execution time Memory
474341 2021-09-18T03:57:39 Z Lam_lai_cuoc_doi Job Scheduling (IOI19_job) C++17
19 / 100
259 ms 15296 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T>
void read(T &x)
{
    x = 0;
    register int c;
    while ((c = getchar()) && (c > '9' || c < '0'))
        ;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
}

constexpr bool typetest = 0;
constexpr int N = 2e5 + 5;
constexpr int Inf = 1e9 + 7;

struct dsu
{
    int par[N];
    dsu()
    {
        memset(par, -1, sizeof par);
    }
    int findpar(int v)
    {
        return par[v] < 0 ? v : par[v] = findpar(par[v]);
    }
} f;

struct Fraction
{
    ll a, b;
    Fraction(ll a = 0, ll b = 1)
    {
        this->a = a;
        this->b = b;
        Normalize();
    }

    inline void Normalize()
    {
        ll v = __gcd(a, b);
        a /= v;
        b /= v;

        if (b < 0)
        {
            a = -a;
            b = -b;
        }
    }

    bool operator<(const Fraction &x) const
    {
        return a * x.b < b * x.a;
    }
    bool operator==(const Fraction &x) const
    {
        return a * x.b == b * x.a;
    }
};

ll scheduling_cost(vector<int> p, vector<int> u, vector<int> d)
{
    int n = p.size();
    /*if (count(p.begin(), p.end(), 0) == n - 1)
    {

        vector<int> id(n);
        for (int i = 0; i < n; ++i)
            id[i] = i;
        sort(id.begin() + 1, id.end(), [&](const int &x, const int &y)
             { return d[x] * u[y] < d[y] * u[x]; });

        ll ans(0), time(0);
        for (int i = 0; i < n; ++i)
        {
            time += d[id[i]];
            //cout << id[i] << " " << time << "\n";
            ans += u[id[i]] * time;
        }
        return ans;
    }*/

    ll ans(0), time(0);
    vector<Fraction> ud(n);

    struct Tque
    {
        Fraction x;
        int v;
        Tque(Fraction x = Fraction(0, 1), ll v = 0) : x(x), v(v) {}

        bool operator<(const Tque &a) const
        {
            return x < a.x || (x == a.x && v > a.v);
        }
    };

    priority_queue<Tque> q;

    for (int i = 0; i < n; ++i)
    {
        ud[i] = Fraction(u[i], d[i]);
        q.emplace(ud[i], i);
    }

    while (!q.empty())
    {
        Tque c = q.top();
        q.pop();

        if (!(c.x == ud[c.v]))
            continue;

        if (p[c.v] == -1 || ud[f.findpar(p[c.v])] == Fraction(-1, 1))
        {
            time += d[c.v];
            ans += time * u[c.v];
            ud[c.v] = Fraction(-1, 1);
        }
        else
        {
            int v = f.findpar(p[c.v]);
            ans -= u[v] * d[c.v];

            u[v] += u[c.v];
            d[v] += d[c.v];
            ud[v] = Fraction(u[v], d[v]);

            f.par[c.v] = v;

            q.emplace(ud[v], v);
        }
    }

    return ans;
}

void Read()
{
    int n;
    cin >> n;
    vector<int> p(n), d(n), u(n);
    for (int i = 0; i < n; ++i)
        cin >> p[i] >> u[i] >> d[i];

    cout << scheduling_cost(p, u, d);
}

void Solve()
{
}

Compilation message

job.cpp: In function 'void read(T&)':
job.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |     register int c;
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Incorrect 1 ms 972 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 137 ms 15168 KB Output is correct
5 Correct 127 ms 15156 KB Output is correct
6 Correct 131 ms 15184 KB Output is correct
7 Correct 136 ms 15172 KB Output is correct
8 Correct 130 ms 15184 KB Output is correct
9 Correct 133 ms 15176 KB Output is correct
10 Correct 130 ms 15168 KB Output is correct
11 Correct 142 ms 15164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 1100 KB Output is correct
5 Correct 7 ms 1936 KB Output is correct
6 Correct 149 ms 15180 KB Output is correct
7 Correct 152 ms 15184 KB Output is correct
8 Correct 142 ms 15284 KB Output is correct
9 Correct 144 ms 15160 KB Output is correct
10 Correct 1 ms 972 KB Output is correct
11 Correct 2 ms 1100 KB Output is correct
12 Correct 6 ms 1936 KB Output is correct
13 Correct 7 ms 1936 KB Output is correct
14 Correct 138 ms 15156 KB Output is correct
15 Correct 147 ms 15296 KB Output is correct
16 Correct 140 ms 15164 KB Output is correct
17 Correct 137 ms 15168 KB Output is correct
18 Correct 136 ms 15180 KB Output is correct
19 Correct 140 ms 15176 KB Output is correct
20 Correct 139 ms 15188 KB Output is correct
21 Correct 180 ms 15208 KB Output is correct
22 Correct 140 ms 15160 KB Output is correct
23 Correct 137 ms 15180 KB Output is correct
24 Correct 145 ms 15172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Incorrect 259 ms 15160 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Incorrect 1 ms 972 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Incorrect 1 ms 972 KB Output isn't correct
4 Halted 0 ms 0 KB -