Submission #1160510

#TimeUsernameProblemLanguageResultExecution timeMemory
1160510TymondJob Scheduling (IOI19_job)C++20
100 / 100
267 ms21572 KiB
#include "job.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_hash{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

vi rep;
int Find(int a){
	if(a == -1 || rep[a] == a){
		return a;
	}
	return rep[a] = Find(rep[a]);
}

ll scheduling_cost(vi p, vi u, vi d){
	int n = sz(p);
	rep.resize(n);
	for(int i = 0; i < n; i++){
		rep[i] = i;
	}

	set<pair<ld, int>> s;
	ll ans = 0LL;
	for(int i = 0; i < n; i++){
		s.insert(mp((ld)u[i] / d[i], i));
		ans += (u[i] * d[i]);
	}
	//cout << ans << '\n';
	
	ll t = 0LL;
	while(sz(s)){
		int v = (*(--s.end())).se;
		s.erase(--s.end());

		p[v] = Find(p[v]);
		//cout << "---------\n";
		//cout << v << ' ' << d[v] << ' ' << u[v] << ' ' << p[v] << '\n';
		if(p[v] == -1){
			ans = (ll)ans + (ll)t * u[v];
			t += d[v];
			d[v] = 0;
			u[v] = 0;
			rep[v] = -1;
		}else{
			//merguj v oraz p[v]
			s.erase(mp((ld)u[p[v]] / d[p[v]], p[v]));
			ans += ((ll)u[v] * d[p[v]]);
			d[v] += d[p[v]];
			u[v] += u[p[v]];
			d[p[v]] = 0;
			u[p[v]] = 0;
			s.insert(mp((ld)u[v] / d[v], v));
			int np = p[p[v]];
			rep[p[v]] = v;
			p[v] = np;
		}
		// cout << ans << '\n';
		// for(int i = 0; i < n; i++){
		// 	cout << i << ' ' << d[i] << ' ' << u[i] << ' ' << Find(p[i]) << '\n';
		// }
	}

	return ans;
}

// int main(){
// 	cout << scheduling_cost({-1, 0, 1, 2}, {5, 2, 5, 3}, {3, 4, 1, 7});
// }
#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...