Submission #144779

#TimeUsernameProblemLanguageResultExecution timeMemory
144779gmfabatJob Scheduling (IOI19_job)C++14
100 / 100
307 ms15916 KiB
#include <bits/stdc++.h>
#include "job.h"
#define ll long long
using namespace std;
const int N = 200005;

int a[N], fa[N], dd[N],sz[N]; ll w[N];

struct node {
    int id; ll son, mom;
    node (int _id = 0, ll _son = 0, ll _mom = 0) {id = _id, son = _son; mom = _mom; }
    bool operator < (const node &b) const {return son*b.mom < b.son*mom; }
};

priority_queue<node>Q;

int find(int o) {return ~fa[o] ? fa[o] = find(fa[o]) : o;}

ll scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d)
{
    int n=p.size();
    memset(fa, -1, sizeof(fa));

    for (int i = 1; i <= n; i++) {  //初始化index , 由1-N 而不是0- N-1, 其實也可不做 , 但下面要小改, 注意index
     a[i]=p[i-1]+1;
     w[i]=u[i-1];
     dd[i]=d[i-1];
    }

    for (int i = 1; i <= n; i++)
       if (find(a[i])^find(i)) fa[find(a[i])] = find(i);

    long long ans = 0; int loc = 0;
    for (int i = 1; i <= n; i++)  scanf("%lld", &w[i]);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &dd[i]);
        Q.push(node(i, w[i], dd[i])); //建堆
        sz[i] = dd[i];
        ans += w[i]*dd[i];
    }
    memset(fa, -1, sizeof(fa));
    while (!Q.empty()) {    // 每次合二個點合二為一, 消一個點和對應邊 , 消到堆為空為止
        node t = Q.top(); Q.pop();
        if (sz[t.id] != t.mom) continue;
        if (find(a[t.id]) == 0) {
            ans += w[t.id]*loc; fa[t.id] = 0; loc += sz[t.id];
        }else {
            int tmp = find(a[t.id]);
            ans += w[t.id]*sz[tmp], fa[t.id] = tmp;
            w[tmp] += w[t.id], sz[tmp] += sz[t.id];
            Q.push(node(tmp, w[tmp], sz[tmp]));
        }
    }
   return ans;
}

Compilation message (stderr)

job.cpp: In function 'long long int scheduling_cost(std::vector<int>, std::vector<int>, std::vector<int>)':
job.cpp:34:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 1; i <= n; i++)  scanf("%lld", &w[i]);
                                   ~~~~~^~~~~~~~~~~~~~~
job.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &dd[i]);
         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...