Submission #210948

# Submission time Handle Problem Language Result Execution time Memory
210948 2020-03-19T03:23:08 Z Kevin_Zhang_TW Job Scheduling (IOI19_job) C++17
0 / 100
25 ms 19584 KB
#include "job.h"
#include <bits/extc++.h>
#include <vector>
#define pb emplace_back
const int maxn = 200010;
using ll = long long;
using namespace std;
int deg[maxn], togo[maxn], tl, n;
ll res;
vector<int> edge[maxn], p, u, d, sorted[maxn];
struct cmp {
	bool operator()(int a, int b) const {
		return d[a] * u[b] > d[b] * u[a];
	}
};
void sub_merge(int a, int b) {
	res -= (ll)d[b] * u[a];
	d[a] += d[b];
	u[a] += u[b];
}
//__gnu_pbds::priority_queue<int, cmp> pq[maxn];
void get_ans() {
	for (int i = 0;i < n;++i) {
		int now = togo[i];
		//cerr << "now " << now << '\n';
		assert(now || i != n-1);
		for (int u : edge[now]) {
			if (sorted[u].size() > sorted[now].size()) swap(sorted[u], sorted[now]);
			int a = sorted[now].size();
			sorted[now].insert(sorted[now].begin(), sorted[u].begin(), sorted[u].end());
			inplace_merge(sorted[now].begin(), sorted[now].begin()+a, sorted[now].end(), cmp());
		}
		while (sorted[now].size() && cmp()(now, sorted[now].back())) {
			sub_merge(now, sorted[now].back());
			sorted[now].pop_back();
		}
		sorted[now].pb(now);
	}
	auto &v = sorted[0];
	ll t = 0;
	auto dojob = [&](int a) {
		//cerr << "dojob " << a << ' ' << "d, u : " << d[a] << ' ' << u[a] << '\n';
		t += d[a];
		res += t * u[a];
	};
	while (v.size()) {
		dojob(v.back());
		v.pop_back();
	}
}
//
//void dfs(int now) {
//	for (int u : edge[i]) dfs(u);
//	sort(edge[now].begin(), edge[now].end(), cmp());
//	vector<int> down;
//	while (edge[now].size() && cmp(now, edge[now].back())) {
//		for (int u : merge(now, edge[now].back()))
//			down.pb(u);
//		edge[now].pop_back();
//	}
//	for (int u : down) edge[now].pb(u);
//}
//
			
long long scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d) {
	n = p.size();
	for (int i = 1;i < n;++i) edge[p[i]].pb(i);
	for (int i = 1;i < n;++i) ++deg[ p[i] ];
	for (int i = 0;i < n;++i) if (deg[i] == 0)
		togo[tl++] = i;
	for (int i = 0;i < n;++i) {
		int now = togo[i];
		if (now == 0) break;
		if ( --deg[ p[now] ] == 0) togo[tl++] = p[now];
	}
	swap(p, ::p), swap(u, ::u), swap(d, ::d);
	get_ans();
	return res;
}
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 19456 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 19456 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 19584 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 19456 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 21 ms 19584 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 19456 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -