Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using let = int;
- using ld = long double;
- using plet = pair<let, let>;
- using vlet = vector<let>;
- using vplet = vector<plet>;
- #define pb push_back
- #define FOR(i, m, n) for (let i(m); i < n; i++)
- #define REP(i, n) FOR(i, 0, n)
- #define F(n) REP(i, n)
- #define FF(n) REP(j, n)
- struct d_ {
- template<typename T> d_ & operator ,(const T & x) { cerr << ' ' << x; return *this;}
- template<typename T> d_ & operator ,(const vector<T> & x) { for(auto x: x) cerr << ' ' << x; return *this;}
- } d_t;
- #define D(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
- #define dbg(args ...) D(args)
- #define EPS (1e-10)
- #define INF (1<<30)
- #define CL(A,I) (memset(A,I,sizeof(A)))
- #define akk(x) begin(x),end(x)
- #define x first
- #define y second
- // ---
- struct Endpoint;
- struct Request;
- let V,E,R,C,X;
- vlet videoSizes;
- vlet cacheSize;
- vector<Endpoint> endpoints;
- vector<Request> requests;
- vector<unordered_set<let>> cacheVideos;
- // ---
- struct CacheConnection {
- let cacheid;
- let LatC;
- };
- struct EndpVideo {
- let vId;
- let num;
- };
- struct endpVideosCmp {
- bool operator()(EndpVideo & a, EndpVideo & b) {
- return (double)a.num/videoSizes[a.vId] < (double)b.num/videoSizes[b.vId];
- }
- };
- struct Endpoint{
- let LatD;
- vector<CacheConnection> caches;
- priority_queue<EndpVideo,vector<EndpVideo>,endpVideosCmp> videos;
- let num;
- let id() {
- return this-&*endpoints.begin();
- }
- };
- struct Request {
- let videoId, endpointId, num;
- };
- struct endpCmp {
- bool operator()(Endpoint * a,Endpoint * b) {
- return (a->num * a->LatD) < (b->num * b->LatD);
- }
- };
- priority_queue<Endpoint*,vector<Endpoint*>,endpCmp> q;
- // ---
- int main(int argc, const char * argv[]) {
- string file = "in";
- if(argc > 1) {
- file = argv[1];
- }
- ifstream cin(file);
- cin >> V>>E>>R>>C>>X;
- cacheVideos = vector<unordered_set<let>>(C);
- F(V) {
- let x;cin>>x; videoSizes.pb(x);
- }
- cacheSize = vlet(C,X);
- F(E) {
- Endpoint e; cin>>e.LatD;
- let K; cin>>K; F(K) {
- let id,Lc; cin>>id>>Lc;
- e.caches.pb({id,Lc});
- }
- endpoints.pb(e);
- }
- F(R) {
- Request r; cin>>r.videoId>>r.endpointId>>r.num;
- requests.pb(r);
- endpoints[r.endpointId].num += r.num; // num of request for videos from this endpoint
- }
- // 1. put endpoints in priority queue, sorted by number of (left) requests times latency
- for(auto & e: endpoints) {
- q.push(&e);
- }
- // and put videos into endpoints
- for(auto & r: requests) {
- endpoints[r.endpointId].videos.push({r.videoId, r.num});
- }
- // 2. while queue not empty
- while(q.size()) {
- // 3. choose top endpoint
- auto curEndpoint = q.top(); q.pop();
- // dbg("take endpoint", curEndpoint->id());
- // take most demanding video from endpoint (stored in priority queue), possibly weighted by size
- if (!curEndpoint->videos.size()) continue;
- auto curVideo = curEndpoint->videos.top(); curEndpoint->videos.pop();
- // dbg("video",curVideo.vId);
- // 4. put into biggest / least demanded chache (if possible)
- double best = -INF; let bestCache=-1; bool isInAny = false;
- for(auto & con : curEndpoint->caches) {
- double cacheVal = (double)cacheSize[con.cacheid] / con.LatC;
- // dbg(con.cacheid,"cacheVal",cacheVal);
- if(cacheSize[con.cacheid] < videoSizes[curVideo.vId]) continue; // don't fit
- if(cacheVal > best) {
- best = cacheVal;
- bestCache = con.cacheid;
- }
- if(cacheVideos[con.cacheid].count(curVideo.vId))
- isInAny = true;
- }
- // dbg(bestCache, isInAny);
- if (!isInAny && bestCache != -1) { // PUT IT IN
- cacheVideos[bestCache].insert(curVideo.vId);
- curEndpoint->num -= curVideo.num;
- cacheSize[bestCache] -= videoSizes[curVideo.vId];
- }
- // 5. put endpoint into queue if has any video left
- if(curEndpoint->videos.size())
- q.push(curEndpoint);
- }
- // OUT:
- cout << cacheVideos.size() << endl;
- F(C) {
- cout << i << ' ';
- for(auto & v : cacheVideos[i]) {
- cout << v << ' ';
- }
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement