Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef long double ld;
  8.  
  9. const ld PI = acos(-1.0);
  10. const ll LINF = (ll)1e18 + 5;
  11. const int INF = (int)1e9 + 5;
  12.  
  13. template<class T>
  14. T sqr(T x) { return x * x; }
  15.  
  16. template<class T>
  17. T abs(T x) { return x < 0 ? -x : x; }
  18.  
  19. template<class T>
  20. ll round(T x) { return x < 0 ? x - 0.5 : x + 0.5; }
  21.  
  22. template<class T>
  23. bool chmin(T & x, const T & y) {
  24.     if (y < x) {
  25.         x = y;
  26.         return true;
  27.     }
  28.     return false;
  29. }
  30.  
  31. template<class T>
  32. bool chmax(T & x, const T & y) {
  33.     if (x < y) {
  34.         x = y;
  35.         return true;
  36.     }
  37.     return false;
  38. }
  39.  
  40.  
  41. template<class P, class Q>
  42. ostream & operator <<(ostream & os, const pair<P, Q> & p) {
  43.     return os << "(" << p.first << ", " << p.second << ")";
  44. }
  45.  
  46. template<class T>
  47. ostream & operator <<(ostream & os, const vector<T> & v) {
  48.     bool was = false;
  49.     os << "{";
  50.     for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {
  51.         if (was) {
  52.             os << ", ";
  53.         }
  54.         else {
  55.             was = true;
  56.         }
  57.         os << *it;
  58.     }
  59.     os << "}";
  60.     return os;
  61. }
  62.  
  63. template<class T>
  64. ostream & operator <<(ostream & os, const set<T> & v) {
  65.     bool was = false;
  66.     os << "{";
  67.     for (typename set<T>::const_iterator it = v.begin(); it != v.end(); it++) {
  68.         if (was) {
  69.             os << ", ";
  70.         }
  71.         else {
  72.             was = true;
  73.         }
  74.         os << *it;
  75.     }
  76.     os << "}";
  77.     return os;
  78. }
  79.  
  80. template<class T>
  81. ostream & operator <<(ostream & os, const multiset<T> & v) {
  82.     bool was = false;
  83.     os << "{";
  84.     for (typename multiset<T>::const_iterator it = v.begin(); it != v.end(); it++) {
  85.         if (was) {
  86.             os << ", ";
  87.         }
  88.         else {
  89.             was = true;
  90.         }
  91.         os << *it;
  92.     }
  93.     os << "}";
  94.     return os;
  95. }
  96.  
  97. template<class P, class Q>
  98. ostream & operator <<(ostream & os, const map<P, Q> & v) {
  99.     bool was = false;
  100.     os << "{";
  101.     for (typename map<P, Q>::const_iterator it = v.begin(); it != v.end(); it++) {
  102.         if (was) {
  103.             os << ", ";
  104.         }
  105.         else {
  106.             was = true;
  107.         }
  108.         os << *it;
  109.     }
  110.     os << "}";
  111.     return os;
  112. }
  113.  
  114. #define all(x) (x).begin(), (x).end()
  115.  
  116. string nextToken() {
  117.     static char str[(int)1e6 + 5];
  118.     scanf("%s", str);
  119.     return str;
  120. }
  121.  
  122. template<class T>
  123. T nextInt() {
  124.     ll x = 0;
  125.     bool p = false;
  126.     char c;
  127.     do {
  128.         c = getchar();
  129.     } while (c <= 32);
  130.     if (c == '-') {
  131.         p = true;
  132.         c = getchar();
  133.     }
  134.     while (c >= '0' && c <= '9') {
  135.         x = x * 10 + c - '0';
  136.         c = getchar();
  137.     }
  138.     return (p ? -x : x);
  139. }
  140.  
  141. struct my {
  142.     ll id, cost;
  143.     my() {}
  144. };
  145.  
  146. struct req {
  147.     ll id, endp, freq;
  148.     req() {}
  149. };
  150.  
  151. ostream& operator <<(ostream& os, const req& r) {
  152.     return os << "(id=" << r.id << ",endp=" << r.endp << ",freq=" << r.freq << ")";
  153. }
  154.  
  155. ostream& operator <<(ostream& os, const my& r) {
  156.     return os << "(id=" << r.id << ",cost=" << r.cost <<")";
  157. }
  158.  
  159. int nVideos, nEndpoints, nRequests, nCaches, capacity;
  160. vector<int> videoSz, videoSzNew;
  161. vector<ll> toDataCenter;
  162. // <ID, cost>
  163. vector<vector<my>> toCacheServer;
  164. vector<ll> minToCacheServer;
  165. vector<req> requests;
  166. int whatPower[2020];
  167. vector<int> blocks[10];
  168. vector<ll> howPopular;
  169. vector<vector<int>> whatRequests;
  170.  
  171. void calcWhatPower() {
  172.     for (int i = 0; i < 10; i++) {
  173.         whatPower[1 << i] = i;
  174.     }
  175. }
  176.  
  177. unsigned long long rand64() {
  178.     unsigned long long res = 0;
  179.     for (int i = 0; i < 4; i++) {
  180.         res <<= 16;
  181.         res ^= rand();
  182.     }
  183.     return res;
  184. }
  185.  
  186. ll rand(ll l, ll r) {
  187.     assert(l <= r);
  188.     return rand64() % (r - l + 1) + l;
  189. }
  190.  
  191. vector<vector<int>> fillCaches(vector<int> order) {
  192.     vector<vector<int>> res(nCaches);
  193.     vector<int> remain(nCaches, capacity);
  194.     vector<set<int>> was(nCaches);
  195.     auto addVideoToCache = [&](int idx) {
  196.         vector<int> cur = whatRequests[idx];
  197.         sort(all(cur), [](int i, int j) {
  198.            return abs(minToCacheServer[requests[i].endp] - toDataCenter[requests[i].endp]) * 1LL * requests[i].freq >
  199.                    abs(minToCacheServer[requests[j].endp] - toDataCenter[requests[j].endp]) * 1LL * requests[j].freq;
  200.         });
  201.         for (int r : cur) {
  202.             req rq = requests[r];
  203.             int endp = rq.endp;
  204.             bool satisfied = false;
  205.             for (auto p : toCacheServer[endp]) {
  206.                 if (was[p.id].count(idx)) {
  207.                     satisfied = true;
  208.                     break;
  209.                 } else if (remain[p.id] >= videoSz[idx]) {
  210.                     remain[p.id] -= videoSz[idx];
  211.                     was[p.id].insert(idx);
  212.                     res[p.id].push_back(idx);
  213.                     satisfied = true;
  214.                     break;
  215.                 }
  216.             }
  217.             if (satisfied) break;
  218.         }
  219.     };
  220.     for (int idx : order) {
  221.         addVideoToCache(idx);
  222.     }
  223.     return res;
  224. }
  225.  
  226. ll estimate(vector<vector<int>> caches) {
  227.     vector<set<int>> was(nCaches);
  228.     for (int i = 0; i < nCaches; i++) {
  229.         was[i] = set<int>(all(caches[i]));
  230.     }
  231.     ll res = 0;
  232.     for (req r : requests) {
  233.         bool satisfied = false;
  234.         for (auto p : toCacheServer[r.endp]) {
  235.             if (was[p.id].count(r.id)) {
  236.                 res += p.cost * 1LL * r.freq;
  237.                 satisfied = true;
  238.                 break;
  239.             }
  240.         }
  241.         if (!satisfied) {
  242.             res += toDataCenter[r.endp] * 1LL * r.freq;
  243.         }
  244.     }
  245.     return res;
  246. }
  247.  
  248. char* dest;
  249.  
  250. void printCaches(vector<vector<int>> caches) {
  251.     ofstream data(dest);
  252.     data << caches.size() << endl;
  253.     for (int i = 0; i < (int)caches.size(); i++) {
  254.         data << i;
  255.         for (int c : caches[i]) {
  256.             data << " " << c;
  257.         }
  258.         data << endl;
  259.     }
  260.     data.close();
  261. }
  262.  
  263. int main(int argc, char * argv[]) {
  264.     srand(time(0));
  265.  
  266.     cerr << argv[1] << endl;
  267.  
  268.     dest = argv[2];
  269.  
  270.     freopen(argv[1], "r", stdin);
  271.     //freopen("output.txt", "w", stdout);
  272.  
  273.     cin >> nVideos >> nEndpoints >> nRequests >> nCaches >> capacity;
  274.     videoSz.resize(nVideos);
  275.  
  276.     for (int& x : videoSz) {
  277.         cin >> x;
  278.     }
  279.  
  280.     cerr << "kittens" << endl;
  281.  
  282.     toDataCenter.resize(nEndpoints);
  283.     toCacheServer.resize(nEndpoints);
  284.  
  285.     minToCacheServer.resize(nEndpoints);
  286.  
  287.     cerr << "!!!" << endl;
  288.  
  289.  
  290.     for (int i = 0; i < nEndpoints; i++) {
  291.         cin >> toDataCenter[i];
  292.         int cnt;
  293.         cin >> cnt;
  294.         toCacheServer[i].resize(cnt);
  295.         for (auto& p : toCacheServer[i]) {
  296.             cin >> p.id >> p.cost;
  297.         }
  298.         sort(all(toCacheServer[i]), [](const my& a, const my& b) {
  299.             return a.cost < b.cost;
  300.         });
  301.         ll minimal = LINF;
  302.         for (auto p : toCacheServer[i]) {
  303.             minimal = min(minimal, p.cost);
  304.         }
  305.     }
  306.  
  307.     cerr << "toDataCenter = " << toDataCenter << endl;
  308.     cerr << "endpoints read" << endl;
  309.  
  310.     requests.resize(nRequests);
  311.  
  312.     howPopular.resize(nRequests);
  313.  
  314.     whatRequests.resize(nVideos);
  315.  
  316.     int curIdx = 0;
  317.     for (auto& z : requests) {
  318.         cin >> z.id >> z.endp >> z.freq;
  319.         howPopular[z.id] += z.freq;
  320.         whatRequests[z.id].push_back(curIdx);
  321.         curIdx++;
  322.     }
  323.  
  324.     cerr << "there is was bug?" << endl;
  325.  
  326.     calcWhatPower();
  327.  
  328.     cerr << "calc what power" << endl;
  329.  
  330.     videoSzNew.resize(nVideos);
  331.  
  332.     for (int i = 0; i < nVideos; i++) {
  333.         videoSzNew[i] = 1;
  334.         while (videoSzNew[i] < videoSz[i]) videoSzNew[i] *= 2;
  335.         //cerr << "videoSzNew[" << i << "] = " << videoSzNew[i] << endl;
  336.         blocks[whatPower[videoSzNew[i]]].push_back(i);
  337.     }
  338.  
  339.     cerr << "blocks filled" << endl;
  340.  
  341.     for (int i = 0; i < 10; i++) {
  342.         sort(all(blocks[i]), [](int i, int j) { return howPopular[i] > howPopular[j]; });
  343.     }
  344.  
  345.     cerr << "blocks created" << endl;
  346.  
  347.     vector<vector<int>> bestCachesStructure;
  348.     ll bestTime = LINF;
  349.     vector<int> bestTop;
  350.    
  351.     auto updAns = [&](vector<vector<int>> caches, vector<int> top, ll est) {
  352.         bestCachesStructure = caches;
  353.         bestTop = top;
  354.         bestTime = est;
  355.     };
  356.  
  357.     auto estimateTop = [&](vector<int> top) {
  358.         vector<int> idxs;
  359.         for (int i = 0; i < 10; i++) {
  360.             for (int z = 0; z < top[i]; z++) {
  361.                 idxs.push_back(blocks[i][z]);
  362.             }
  363.         }
  364.         random_shuffle(all(idxs));
  365.         vector<vector<int>> whatCachesContain = fillCaches(idxs);
  366.         ll curTime = estimate(whatCachesContain);
  367.         if (curTime < bestTime) {
  368.             bestCachesStructure = whatCachesContain;
  369.             bestTime = curTime;
  370.             printCaches(bestCachesStructure);
  371.             bestTop = top;
  372.             return curTime;
  373.         }
  374.         return curTime;
  375.     };
  376.  
  377.     auto getTransProb = [](double de, double t) {
  378.         return exp(-de/t);
  379.     };
  380.  
  381.     vector<int> top(10);
  382.  
  383.     for (int i = 0; i < 10; i++) {
  384.         top[i] = rand(0, min<int>(1e9, blocks[i].size()));
  385.     }
  386.  
  387.     ll curEnergy = estimateTop(top);
  388.     double tempe = 1000;
  389.  
  390.     for (int iter = 0; iter < 1000; iter++) {
  391.         cerr << "iter = " << iter << ", top = " << top << ", est = " << estimateTop(top) << endl;
  392.         auto newTop = top;
  393.         for (int p = 0; p < 10; p++) {
  394.             if (rand(0, 3) == 0) {
  395.                 int howMuch = rand(0, newTop[p]) / 2;
  396.                 newTop[p] -= howMuch;
  397.             } else {
  398.                 int howMuch = rand(0, (int)blocks[p].size() - newTop[p]) / 2;
  399.                 newTop[p] += howMuch;
  400.             }
  401.         }
  402.         cerr << "newTop = " << newTop << endl;
  403.         ll candEnergy = estimateTop(newTop);
  404.         if (candEnergy < curEnergy) {
  405.             curEnergy = candEnergy;
  406.             top = newTop;
  407.         } else {
  408.             double p = getTransProb(candEnergy - curEnergy, tempe);
  409.  
  410.             if (rand(0, (int)1e6) / 1e6 <= p) {
  411.                 curEnergy = candEnergy;
  412.                 top = newTop;
  413.             }
  414.         }
  415.         tempe /= abs(log(tempe));
  416.         if (tempe <= 0.00001) {
  417.             break;
  418.         }
  419.     }
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement