Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using let = int;
  6. using ld = long double;
  7.  
  8. using plet = pair<let, let>;
  9. using vlet = vector<let>;
  10. using vplet = vector<plet>;
  11.  
  12. #define pb push_back
  13. #define FOR(i, m, n) for (let i(m); i < n; i++)
  14. #define REP(i, n) FOR(i, 0, n)
  15. #define F(n) REP(i, n)
  16. #define FF(n) REP(j, n)
  17. struct d_ {
  18. template<typename T> d_ & operator ,(const T & x) { cerr << ' ' << x; return *this;}
  19. template<typename T> d_ & operator ,(const vector<T> & x) { for(auto x: x) cerr << ' ' << x; return *this;}
  20. } d_t;
  21. #define D(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
  22. #define dbg(args ...) D(args)
  23. #define EPS (1e-10)
  24. #define INF (1<<30)
  25. #define CL(A,I) (memset(A,I,sizeof(A)))
  26. #define akk(x) begin(x),end(x)
  27. #define x first
  28. #define y second
  29.  
  30. // ---
  31.  
  32. struct Endpoint;
  33. struct Request;
  34.  
  35. let V,E,R,C,X;
  36. vlet videoSizes;
  37. vlet cacheSize;
  38. vector<Endpoint> endpoints;
  39. vector<Request> requests;
  40. vector<unordered_set<let>> cacheVideos;
  41.  
  42. // ---
  43.  
  44. struct CacheConnection {
  45. let cacheid;
  46. let LatC;
  47. };
  48.  
  49. struct EndpVideo {
  50. let vId;
  51. let num;
  52. };
  53. struct endpVideosCmp {
  54. bool operator()(EndpVideo & a, EndpVideo & b) {
  55. return (double)a.num/videoSizes[a.vId] < (double)b.num/videoSizes[b.vId];
  56. }
  57. };
  58.  
  59. struct Endpoint{
  60. let LatD;
  61. vector<CacheConnection> caches;
  62. priority_queue<EndpVideo,vector<EndpVideo>,endpVideosCmp> videos;
  63. let num;
  64. let id() {
  65. return this-&*endpoints.begin();
  66. }
  67. };
  68.  
  69. struct Request {
  70. let videoId, endpointId, num;
  71. };
  72.  
  73.  
  74. struct endpCmp {
  75. bool operator()(Endpoint * a,Endpoint * b) {
  76. return (a->num * a->LatD) < (b->num * b->LatD);
  77. }
  78. };
  79. priority_queue<Endpoint*,vector<Endpoint*>,endpCmp> q;
  80.  
  81.  
  82. // ---
  83.  
  84. int main(int argc, const char * argv[]) {
  85.  
  86. string file = "in";
  87.  
  88. if(argc > 1) {
  89. file = argv[1];
  90. }
  91.  
  92. ifstream cin(file);
  93.  
  94. cin >> V>>E>>R>>C>>X;
  95. cacheVideos = vector<unordered_set<let>>(C);
  96. F(V) {
  97. let x;cin>>x; videoSizes.pb(x);
  98. }
  99. cacheSize = vlet(C,X);
  100. F(E) {
  101. Endpoint e; cin>>e.LatD;
  102. let K; cin>>K; F(K) {
  103. let id,Lc; cin>>id>>Lc;
  104. e.caches.pb({id,Lc});
  105. }
  106. endpoints.pb(e);
  107. }
  108. F(R) {
  109. Request r; cin>>r.videoId>>r.endpointId>>r.num;
  110. requests.pb(r);
  111. endpoints[r.endpointId].num += r.num; // num of request for videos from this endpoint
  112. }
  113. // 1. put endpoints in priority queue, sorted by number of (left) requests times latency
  114. for(auto & e: endpoints) {
  115. q.push(&e);
  116. }
  117. // and put videos into endpoints
  118. for(auto & r: requests) {
  119. endpoints[r.endpointId].videos.push({r.videoId, r.num});
  120. }
  121. // 2. while queue not empty
  122. while(q.size()) {
  123. // 3. choose top endpoint
  124. auto curEndpoint = q.top(); q.pop();
  125. // dbg("take endpoint", curEndpoint->id());
  126. // take most demanding video from endpoint (stored in priority queue), possibly weighted by size
  127. if (!curEndpoint->videos.size()) continue;
  128. auto curVideo = curEndpoint->videos.top(); curEndpoint->videos.pop();
  129. // dbg("video",curVideo.vId);
  130. // 4. put into biggest / least demanded chache (if possible)
  131. double best = -INF; let bestCache=-1; bool isInAny = false;
  132. for(auto & con : curEndpoint->caches) {
  133. double cacheVal = (double)cacheSize[con.cacheid] / con.LatC;
  134. // dbg(con.cacheid,"cacheVal",cacheVal);
  135. if(cacheSize[con.cacheid] < videoSizes[curVideo.vId]) continue; // don't fit
  136. if(cacheVal > best) {
  137. best = cacheVal;
  138. bestCache = con.cacheid;
  139. }
  140. if(cacheVideos[con.cacheid].count(curVideo.vId))
  141. isInAny = true;
  142. }
  143. // dbg(bestCache, isInAny);
  144. if (!isInAny && bestCache != -1) { // PUT IT IN
  145. cacheVideos[bestCache].insert(curVideo.vId);
  146. curEndpoint->num -= curVideo.num;
  147. cacheSize[bestCache] -= videoSizes[curVideo.vId];
  148. }
  149. // 5. put endpoint into queue if has any video left
  150. if(curEndpoint->videos.size())
  151. q.push(curEndpoint);
  152. }
  153.  
  154. // OUT:
  155. cout << cacheVideos.size() << endl;
  156. F(C) {
  157. cout << i << ' ';
  158. for(auto & v : cacheVideos[i]) {
  159. cout << v << ' ';
  160. }
  161. cout<<endl;
  162. }
  163.  
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement