Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <utility>
  6. #include <cstdlib>
  7. #include <queue>
  8. #include <deque>
  9. #include <list>
  10. #include <set>
  11. #include <map>
  12. #include <vector>
  13. #include <algorithm>
  14. #include <cmath>
  15. #include <cassert>
  16. #include <memory.h>
  17. #include <ctime>
  18. #include <cctype>
  19.  
  20. using namespace std;
  21.  
  22. #define forn(i,n) for (int i = 0; i < int(n); i++)
  23. #define ford(i,n) for (int i = int(n) - 1; i >= 0; i--)
  24. #define forv(i,v) for (typeof(v.begin()) i = v.begin(); i != v.end(); i++)
  25. #define mp make_pair
  26. #define fs first
  27. #define sc second
  28. #define pb push_back
  29. #define min trgjio
  30. #define max fgopip
  31. #define swap kderoe
  32. #define rand eugtr
  33. #define srand tkjrg
  34.  
  35. typedef long double ld;
  36. typedef long long ll;
  37. typedef unsigned char uc;
  38. typedef unsigned int ui;
  39. typedef unsigned long long ull;
  40. typedef pair<int, int> pii;
  41. typedef vector<int> vi;
  42.  
  43. const ld PI = 3.141592653589793238462643l;
  44.  
  45. inline int min(int a, int b) {
  46.     return (a < b) ? a : b;
  47. }
  48.  
  49. inline int max(int a, int b) {
  50.     return (a > b) ? a : b;
  51. }
  52.  
  53. inline void swap(int &a, int &b) {
  54.     int c = a;
  55.     a = b;
  56.     b = c;
  57. }
  58.  
  59. int lastrand = 0;
  60.  
  61. inline void srand(int a) {
  62.     lastrand = a;
  63. }
  64.  
  65. inline int rand() {
  66.     return abs((lastrand *= 1000000009) += 31);
  67. }
  68.  
  69. void shuffle(vector<int> &a) {
  70.     forn (i, a.size()) {
  71.         swap(a[i], a[rand() % (i + 1)]);
  72.     }
  73. }
  74.  
  75. vector<int> vlist(int n) {
  76.     vector<int> a(n);
  77.     forn (i, n) {
  78.         a[i] = i;
  79.     }
  80.     return a;
  81. }
  82.  
  83. void remove(vector<int> &a, int i) {
  84.     swap(a[i], a[a.size() - 1]);
  85.     a.pop_back();
  86. }
  87.  
  88. int gcd(int a, int b) {
  89.     return a ? gcd(b % a, a) : b;
  90. }
  91.  
  92. struct rect {
  93.     int w, h;
  94.     int zmn, zmx;
  95.  
  96.     rect() {}
  97.  
  98.     rect(int n, int m) {
  99.         int d = gcd(n, m);
  100.         w = n / d;
  101.         h = m / d;
  102.         d /= 10;
  103.         zmn = d;
  104.         zmx = 20 * d;
  105.     }
  106.  
  107.     inline int getw(bool x) {
  108.         return x ? h : w;
  109.     }
  110.  
  111.     inline int geth(bool x) {
  112.         return x ? w : h;
  113.     }
  114. };
  115.  
  116. struct pos {
  117.     rect r;
  118.     bool put;
  119.     bool rot;
  120.     int x1, y1, x2, y2, z;
  121.    
  122.     inline void upd() {
  123.         if  (put) {
  124.             if (rot) {
  125.                 x2 = x1 + r.h * z;
  126.                 y2 = y1 + r.w * z;
  127.             } else {
  128.                 x2 = x1 + r.w * z;
  129.                 y2 = y1 + r.h * z;
  130.             }
  131.         }
  132.     }
  133.  
  134.     inline void ch() {
  135.         if (x1 > x2) {
  136.             swap(x1, x2);
  137.         }
  138.         if (y1 > y2) {
  139.             swap(y1, y2);
  140.         }
  141.         int w = y2 - y1;
  142.         int h = x2 - x1;
  143.         rot = (r.w > r.h) ^ (w > h);
  144.         z = w / r.getw(rot);
  145.     }
  146.  
  147.     pos() {
  148.         put = 0;
  149.         z = 1;
  150.     }
  151.  
  152.     pos(rect a) {
  153.         r = a;
  154.         put = 0;
  155.         z = 1;
  156.     }
  157.  
  158.     pos(int n, int m) {
  159.         r = rect(n, m);
  160.         put = 0;
  161.         z = 1;
  162.     }
  163.  
  164.     inline void inv() {
  165.         swap(x1, y1);
  166.         swap(x2, y2);
  167.         rot ^= 1;
  168.     }
  169.  
  170.     inline void move(int x, int y) {
  171.         x1 += x;
  172.         x2 += x;
  173.         y1 += y;
  174.         y2 += y;
  175.     }
  176.  
  177.     inline int getw() {
  178.         return r.getw(rot) * z;
  179.     }
  180.  
  181.     inline int geth() {
  182.         return r.geth(rot) * z;
  183.     }
  184. };
  185.  
  186. inline int sxs(int l1, int r1, int l2, int r2) {
  187.     return max(0, min(r1, r2) - max(l1, l2));
  188. }
  189.  
  190. inline int score(const pos &a, const pos &b) {
  191.     if (!a.put || !b.put) {
  192.         return 0;
  193.     }
  194.     int l = 0;
  195.     if (a.x1 == b.x2 || a.x2 == b.x1) {
  196.         l = sxs(a.y1, a.y2, b.y1, b.y2);
  197.     } else if (a.y1 == b.y2 || a.y2 == b.y1) {
  198.         l = sxs(a.x1, a.x2, b.x1, b.x2);
  199.     } else {
  200.         return 0;
  201.     }
  202.     if (!(a.rot ^ b.rot)) {
  203.         l = -l;
  204.     }
  205.     return l;
  206. }
  207.  
  208. int scorestupid(const vector<pos> &a) {
  209.     int ans = 0;
  210.     forn (i, a.size()) {
  211.         forn (j, i) {
  212.             int s = score(a[i], a[j]);
  213.             ans += s;
  214.         }
  215.     }
  216.     return ans;
  217. }
  218.  
  219. int score(vector<pos> a) {
  220.     int s = 0;
  221.     forn (b, 2) {
  222.         vector<pair<pair<pair<int, bool>, int>, int> > v;
  223.         forn (i, a.size()) {
  224.             v.pb(mp(mp(mp(a[i].x1, 0), a[i].y2), i));
  225.             v.pb(mp(mp(mp(a[i].x2, 1), a[i].y2), i));
  226.         }
  227.         sort(v.begin(), v.end());
  228.         for (int i = 0; i < (int)v.size(); ) {
  229.             int j = i;
  230.             while (j < (int)v.size() && v[j].fs.fs.fs == v[i].fs.fs.fs && v[j].fs.fs.sc == 0) {
  231.                 j++;
  232.             }
  233.             int k = j;
  234.             while (k < (int)v.size() && v[k].fs.fs.fs == v[i].fs.fs.fs) {
  235.                 k++;
  236.             }
  237.             int l = i, r = j;
  238.             while (l < j && r < k) {
  239.                 s += score(a[v[l].sc], a[v[r].sc]);
  240.            
  241.                 if (v[l].fs.sc < v[r].fs.sc) {
  242.                     l++;
  243.                 } else {
  244.                     r++;
  245.                 }
  246.             }
  247.             i = k;
  248.         }
  249.         forn (i, a.size()) {
  250.             a[i].inv();
  251.         }
  252.     }
  253.     return s;
  254. }
  255.  
  256. struct alloc {
  257.     int w, h;
  258.     vector<pos> p;
  259.     int s;
  260.  
  261.     alloc() {
  262.         s = -1000000000;
  263.     }
  264.  
  265.     alloc(int x, int y, const vector<pos> &a) {
  266.         w = x;
  267.         h = y;
  268.         p = a;
  269.         s = score(a);
  270.     }
  271.  
  272.     alloc(int x, int y, const vector<pos> &a, int c) {
  273.         w = x;
  274.         h = y;
  275.         p = a;
  276.         s = c;
  277.     }
  278.    
  279.     inline alloc& bst(const alloc &a) {
  280.         if (a.s > s) {
  281.             *this = a;
  282.         }
  283.         return *this;
  284.     }
  285.  
  286.     inline alloc& inv() {
  287.         swap(w, h);
  288.         forn (i, p.size()) {
  289.             p[i].rot ^= 1;
  290.             swap(p[i].x1, p[i].y1);
  291.             p[i].upd();
  292.         }
  293.         return *this;
  294.     }
  295.    
  296.     inline void refh() {
  297.         forn (i, p.size()) {
  298.             p[i].x1 = w - p[i].x1;
  299.             p[i].x2 = w - p[i].x2;
  300.             swap(p[i].x1, p[i].x2);
  301.         }
  302.     }
  303.    
  304.     inline void refv() {
  305.         forn (i, p.size()) {
  306.             p[i].y1 = h - p[i].y1;
  307.             p[i].y2 = h - p[i].y2;
  308.             swap(p[i].y1, p[i].y2);
  309.         }
  310.     }
  311.    
  312.     inline alloc& rot(int k) {
  313.         k &= 3;
  314.         if (k == 2) {
  315.             refh();
  316.             refv();
  317.         } else if (k == 1) {
  318.             inv();
  319.             refh();
  320.         } else if (k == 3) {
  321.             inv();
  322.             refv();
  323.         }
  324.         return *this;
  325.     }
  326. };
  327.  
  328. bool operator<(const alloc &a, const alloc &b) {
  329.     return a.s < b.s;
  330. }
  331.  
  332. vector<pos> *srtv;
  333.  
  334. bool longer(int a, int b) {
  335.     return (*srtv)[a].geth() * (*srtv)[b].getw() > (*srtv)[b].geth() * (*srtv)[a].getw();
  336. }
  337.  
  338. alloc optimize1(const alloc &t) {
  339.     int www = t.w;
  340.     int ps = t.s;
  341.     vector<pos> a = t.p;
  342.     vector<pair<pair<int, bool>, int> > v;
  343.     vector<int> d;
  344.     vector<int> num;
  345.     forn (i, a.size()) {
  346.         if (a[i].put) {
  347.             v.pb(mp(mp(a[i].x1, 1), i));
  348.             v.pb(mp(mp(a[i].x2, 0), i));
  349.             d.pb(t.h - a[i].y2);
  350.         } else {
  351.             d.pb(-1);
  352.             num.pb(i);
  353.         }
  354.     }
  355.     sort(v.begin(), v.end());
  356.     sort(num.begin(), num.end());
  357.     set<pair<int, int> > s;
  358.     forn (i, v.size() - 1) {
  359.         int k = v[i].sc;
  360.         if (v[i].fs.sc) {
  361.             s.insert(mp(a[k].y1, k));
  362.         } else {
  363.             s.erase(mp(a[k].y1, k));
  364.         }
  365.         if (s.size()) {
  366.             for (set<pair<int, int> >::iterator i1 = s.begin(), i2 = ++s.begin(); i2 != s.end(); i1 = i2++) {
  367.                 int u1 = i1->sc, u2 = i2->sc;
  368.                 d[u1] = min(d[u1], a[u2].y1 - a[u1].y2 - 1);
  369.             }
  370.         }
  371.     }
  372.  
  373.     vector<pair<int, int> > l;
  374.     forn (i, a.size()) {
  375.         if (a[i].put) {
  376.             l.pb(mp(d[i], i));
  377.         }
  378.     }
  379.     sort(l.begin(), l.end());
  380.     do {
  381.         int q1 = rand() % (1 + rand() % l.size());
  382.         int q2 = rand() % l.size();
  383.         swap(l[q1].fs, l[q2].fs);
  384.         swap(l[q1].sc, l[q2].sc);
  385.     } while (rand() % 10 == 0);
  386.     ford (qwe, l.size()) {
  387.         int i = l[qwe].sc;
  388.         int H = d[i], W = a[i].x2 - a[i].x1 - 2;
  389.         if (H > 0 && W > 0) {
  390.             int x0 = a[i].x1 + 1;
  391.             int y0 = a[i].y2;
  392.             bool q = a[i].rot;
  393.             int last = i;
  394.             while (1) {
  395.                 q ^= 1;
  396.                 int chi = -1, chz = 0, sc = -1000000000;
  397.                 forn (k, num.size()) {
  398.                     pos &t = a[num[k]];
  399.                     int w = t.r.getw(q);
  400.                     int h = t.r.geth(q);
  401.                     int lz = t.r.zmn, rz = t.r.zmx;
  402.                     rz = min(rz, W / w);
  403.                     rz = min(rz, H / h);
  404.                     if (lz > rz) {
  405.                         continue;
  406.                     }
  407.                     w *= rz;
  408.                     h *= rz;
  409.  
  410.                     int s = -((W - w) * H + h * W);
  411.                     if (s > sc) {
  412.                         chi = k;
  413.                         chz = rz;
  414.                         sc = s;
  415.                     }
  416.                 }
  417.                 if (chi < 0) {
  418.                     break;
  419.                 }
  420.                 pos t = a[num[chi]];
  421.                 t.y1 = y0;
  422.                 t.z = chz;
  423.                 t.put = 1;
  424.                 t.rot = q;
  425.                 t.x1 = x0;
  426.                 t.upd();
  427.                 int last1 = num[chi];
  428.                 int lb = 0, rb = www;
  429.                 vector<int> lv, rv;
  430.                 forn (i, a.size()) {
  431.                     if (a[i].put) {
  432.                         if (sxs(a[i].y1, a[i].y2, t.y1, t.y2)) {
  433.                             if (a[i].x2 <= t.x1) {
  434.                                 if (a[i].x2 > lb) {
  435.                                     lb = a[i].x2;
  436.                                     lv.clear();
  437.                                 }
  438.                                 if (a[i].x2 == lb) {
  439.                                     lv.pb(i);
  440.                                 }
  441.                             }
  442.                             if (a[i].x1 >= t.x2) {
  443.                                 if (a[i].x1 < rb) {
  444.                                     rb = a[i].x1;
  445.                                     rv.clear();
  446.                                 }
  447.                                 if (a[i].x1 == rb) {
  448.                                     rv.pb(i);
  449.                                 }
  450.                             }
  451.                         }
  452.                     }
  453.                 }
  454.                 int x[3];
  455.                 x[0] = x0;
  456.                 x[1] = lb;
  457.                 x[2] = rb - chz * t.r.getw(q);
  458.                 pos bstpos = t;
  459.                 int bstsc = -1000000000;
  460.                 remove(num, chi);
  461.                 forn (i, 3) {
  462.                     t.x1 = x[i];
  463.                     t.upd();
  464.                     int sc = 0;
  465.                     sc += score(t, a[last]);
  466.                     forn (i, lv.size()) {
  467.                         sc += score(t, a[lv[i]]);
  468.                     }
  469.                     forn (i, rv.size()) {
  470.                         sc += score(t, a[rv[i]]);
  471.                     }
  472.                     if (sc > bstsc) {
  473.                         bstsc = sc;
  474.                         bstpos = t;
  475.                     }
  476.                 }
  477.                 forn (i, a.size()) {
  478.                     if (a[i].put && sxs(a[i].x1, a[i].x2, bstpos.x1, bstpos.x2) && a[i].y2 <= bstpos.y1) {
  479.                         d[i] = min(d[i], bstpos.y1 - a[i].y2 - 1);
  480.                     }
  481.                 }
  482.                 a[last1] = bstpos;
  483.                 y0 = t.y2;
  484.                 H -= t.y2 - t.y1;
  485.                 last = last1;
  486.                 ps += bstsc;
  487.             }
  488.         }
  489.     }
  490.     return alloc(t.w, t.h, a);
  491. }
  492.  
  493. alloc optimize1_T(alloc v, int t) {
  494.     if (t) {
  495.         v.inv();
  496.     }
  497.     alloc w = optimize1(v);
  498.     if (t) {
  499.         w.inv();
  500.     }
  501.     return w;
  502. }
  503.  
  504. alloc optimize1_all(alloc w) {
  505.     alloc ans = w;
  506.     forn (q, 2) {
  507.         alloc v = w;
  508.         for (int i = 0; ; i++) {
  509.             int ss = v.s;
  510.             v = optimize1_T(v, (i + q) & 1);
  511.             if (v.s == ss) {
  512.                 break;
  513.             }
  514.         }
  515.         ans.bst(v);
  516.     }
  517.     return ans;
  518. }
  519.  
  520. alloc optimize1_time(alloc v, double TL) {
  521.     alloc ans = v;
  522.     int mxt = 0, time = clock();
  523.     for (int cnt = 0; time + mxt < TL * CLOCKS_PER_SEC; cnt++) {
  524.         int time0 = time;
  525.         ans.bst(optimize1_all(v));
  526.         time = clock();
  527.         mxt = max(mxt, time - time0);
  528.     }
  529.     return ans;
  530. }
  531.  
  532. alloc solve1(const alloc &v, int maxw = -1, int minw = -1, int maxh = -1, int minh = -1) {
  533.     vector<pos> p = v.p;
  534.     int ps = 0;
  535.     int n = p.size();
  536.     if (maxw == -1) {
  537.         maxw = 1 + rand() % (1 + rand() % v.w);
  538.     }
  539.     if (minw == -1) {
  540.         minw = 1 + rand() % (1 + rand() % maxw);
  541.     }
  542.     if (maxh == -1) {
  543.         maxh = 1 + rand() % (1 + rand() % v.h);
  544.     }
  545.     if (minh == -1) {
  546.         minh = 1 + rand() % (1 + rand() % maxh);
  547.     }
  548.     vector<int> num = vlist(n);
  549.     shuffle(num);
  550.    
  551.     vector<int> a;
  552.     bool q = rand() & 1;
  553.     for (int i = 0; ; i++) {
  554.         int x0 = (i ? p[a[i - 1]].x2 : 0), y0 = 0;
  555.         int chi = -1, chz = 0, sc = -1000000000;
  556.         forn (bad, 2) {
  557.             forn (k, num.size()) {
  558.                 pos &t = p[num[k]];
  559.                 int w = t.r.getw(q);
  560.                 int h = t.r.geth(q);
  561.                 int lz = t.r.zmn, rz = t.r.zmx;
  562.                 rz = min(rz, (v.w - x0) / w);
  563.                 rz = min(rz, (v.h - y0) / h);
  564.                 if (!bad) {
  565.                     rz = min(rz, maxw / w);
  566.                     lz = max(lz, (minw - 1) / w + 1);
  567.                 }
  568.                 if (!i) {
  569.                     if (!bad) {
  570.                         rz = min(rz, maxh / h);
  571.                         lz = max(lz, (minh - 1) / h + 1);
  572.                     }
  573.                 } else {
  574.                     rz = min(rz, (p[a[i - 1]].y2 - y0 - 1) / h);
  575.                 }
  576.                 if (lz > rz) {
  577.                     continue;
  578.                 }
  579.                 int z[2] = {lz, rz}, s[2] = {0, 0};
  580.                 forn (g, 2) {
  581.                     s[g] = 0;
  582.                     if (i) {
  583.                         s[g] += z[g] * h;
  584.                     }
  585.                 }
  586.                 int bt = (s[1] > s[0]);
  587.                 if (s[1] == s[0]) {
  588.                     z[bt] = lz + rand() % (rz - lz + 1);
  589.                 }
  590.  
  591.                 if (s[bt] > sc) {
  592.                     chi = k;
  593.                     chz = z[bt];
  594.                     sc = s[bt];
  595.                 }
  596.             }
  597.             if (chi >= 0) {
  598.                 break;
  599.             }
  600.         }
  601.         if (chi < 0) {
  602.             break;
  603.         }
  604.         pos &t = p[num[chi]];
  605.         a.pb(num[chi]);
  606.         remove(num, chi);
  607.         shuffle(num);
  608.         t.x1 = x0;
  609.         t.y1 = y0;
  610.         t.z = chz;
  611.         t.put = 1;
  612.         t.rot = q;
  613.         t.upd();
  614.         if (i) {
  615.             ps += score(t, p[a[i - 1]]);
  616.         }
  617.         q ^= 1;
  618.     }
  619.    
  620.     vector<int> a1;
  621.     while (1) {
  622. //      forn (j, a.size()) {
  623. //          cerr << a[j] << ' ';
  624. //      }
  625. //      cerr << endl;
  626.         a1.clear();
  627.         int i = 0;
  628.         while (i < (int)a.size()) {
  629.             int a1s = a1.size();
  630.             bool q = p[a[i]].rot ^ 1;
  631.             if (a1s) {
  632.                 q = p[a1[a1s - 1]].rot ^ 1;
  633.             }
  634.             bool tan = 1;
  635.             int j = i;
  636.             while (j < (int)a.size() &&p[a[j]].rot == p[a[i]].rot) {
  637.                 j++;
  638.             }
  639.             while (j < (int)a.size() && p[a[j]].rot != q) {
  640.                 j++;
  641.                 tan = 0;
  642.             }
  643.             int j1 = j + 1;
  644.             while (j1 < (int)a.size() && p[a[j]].rot == p[a[j1]].rot) {
  645.                 j1++;
  646.             }
  647.             j--;
  648.             j1--;
  649.             int x0 = (a1.size() ? p[a1[a1s - 1]].x2 : (i ? p[a[i - 1]].x2 + (p[a[i - 1]].rot == q) : 0));
  650.             int y0 = p[a[i]].y2 + (!tan);
  651.  
  652.             int chi = -1, chz = 0, sc = -1000000000;
  653.             forn (bad, 2) {
  654.                 forn (k, num.size()) {
  655.                     pos &t = p[num[k]];
  656.                     int w = t.r.getw(q);
  657.                     int h = t.r.geth(q);
  658.                     int lz = t.r.zmn, rz = t.r.zmx;
  659.                     rz = min(rz, (v.w - x0) / w);
  660.                     rz = min(rz, (v.h - y0) / h);
  661.                    
  662.                     if (!a1s) {
  663.                         if (!bad) {
  664.                             rz = min(rz, maxh / h);
  665.                             lz = max(lz, (minh - 1) / h + 1);
  666.                         }
  667.                     } else {
  668.                         rz = min(rz, (p[a1[a1s - 1]].y2 - y0 - 1) / h);
  669.                     }
  670.                    
  671.                     lz = max(lz, (p[a[j]].x2 - x0) / w + 1);
  672.                     if (!bad) {
  673.                         if (j1 == (int)a.size()) {
  674.                             rz = min(rz, maxw / w);
  675.                             lz = max(lz, (minw - 1) / w + 1);
  676.                         } else {
  677.                             rz = min(rz, (p[a[j1]].x2 - x0 - 1) / w);
  678.                         }
  679.                     }
  680.  
  681.                     if (lz > rz) {
  682.                         continue;
  683.                     }
  684.  
  685.                     int z[2] = {lz, rz}, s[2] = {0, 0};
  686.                     forn (g, 2) {
  687.                         s[g] = 0;
  688.                         s[g] -= z[g] * w;
  689.                         if (a1.size()) {
  690.                             s[g] += z[g] * h;
  691.                         }
  692.                     }
  693.  
  694.                     int bt = (s[1] > s[0]);
  695.                     if (s[bt] > sc) {
  696.                         chi = k;
  697.                         chz = z[bt];
  698.                         sc = s[bt];
  699.                     }
  700.                 }
  701.                 if (chi >= 0) {
  702.                     break;
  703.                 }
  704.             }
  705.             if (chi < 0) {
  706.                 if (a1.size()) {
  707.                     break;
  708.                 } else {
  709.                     i++;
  710.                     continue;
  711.                 }
  712.             }
  713.  
  714.             pos &t = p[num[chi]];
  715.             t.x1 = x0;
  716.             t.y1 = y0;
  717.             t.z = chz;
  718.             t.put = 1;
  719.             t.rot = q;
  720.             t.upd();
  721.             if (!a1s) {
  722.                 forn (e, i) {
  723.                     if (p[a[e]].y2 >= t.y2) {
  724.                         a1.pb(a[e]);
  725.                     }
  726.                 }
  727.             }
  728.             a1.pb(num[chi]);
  729.             remove(num, chi);
  730.             shuffle(num);
  731.             ps += score(t, p[a[i]]);
  732.             if (a1s) {
  733.                 ps += score(t, p[a1[a1s - 1]]);
  734.             }
  735.             while (i < (int)a.size() && p[a[i]].x2 < t.x2) {
  736.                 i++;
  737.             }
  738.         }
  739.         if (!a1.size()) {
  740.             break;
  741.         }
  742.         forn (i, a.size()) {
  743.             if (p[a[i]].x2 > p[a1[a1.size() - 1]].x2) {
  744.                 a1.pb(a[i]);
  745.             }
  746.         }
  747.         a = a1;
  748.     }
  749.     return alloc(v.w, v.h, p);
  750. }
  751.  
  752. alloc solve1_T(alloc v, bool t = -1, int maxw = -1, int minw = -1, int maxh = -1, int minh = -1) {
  753.     if (t == -1) {
  754.         t = rand() & 1;
  755.     }
  756.     if (t) {
  757.         v.inv();
  758.     }
  759.     alloc w = solve1(v, maxw, minw, maxh, minh);
  760.     if (t) {
  761.         w.inv();
  762.     }
  763.     return w;
  764. }
  765.  
  766. alloc main1(const alloc &v0, double TL, int t = -1, int maxw = -1, int minw = -1, int maxh = -1, int minh = -1) {
  767.     alloc ans = v0;
  768.     int mxt = 0, time = clock(), mxo = 10;
  769.     for (int cnt = 0; time + mxt < TL * 0.99 * CLOCKS_PER_SEC; cnt++) {
  770.         alloc v = solve1_T(v0, t, maxw, minw, maxh, minh);
  771.         int s = v.s;
  772.         if (s + 2 * mxo > ans.s) {
  773.             alloc w = optimize1_all(v);
  774.             mxo = max(mxo, v.s - s);
  775.             ans.bst(w);
  776.         }
  777.         int time0 = time;
  778.         time = clock();
  779.         mxt = max(mxt, time - time0);
  780.     }
  781.     return optimize1_time(ans, TL);
  782. }
  783.  
  784. alloc main1_ch(const alloc &v0, double TL, double preTL = -1) {
  785.     if (preTL < -0.5) {
  786.         preTL = TL / 5;
  787.     }
  788.     alloc ans[2];
  789.     forn (i, 2) {
  790.         ans[i] = main1(v0, (i + 1) * preTL / 2, i);
  791.     }
  792.     bool b = (ans[1].s > ans[0].s);
  793.     return main1(v0, TL, b).bst(ans[b]);
  794. }
  795.  
  796. alloc getdata() {
  797.     int k, N, M;
  798.     cin >> N >> M >> k;
  799.     N *= 10;
  800.     M *= 10;
  801.     vector<pos> V(k);
  802.     forn (i, k) {
  803.         int w, h;
  804.         cin >> w >> h;
  805.         V[i] = pos(10 * w, 10 * h);
  806.     }
  807.     return alloc(N, M, V, 0);
  808. }
  809.  
  810. void write(const alloc &a) {
  811.     forn (i, a.p.size()) {
  812.         if (!a.p[i].put) {
  813.             puts("-1 -1 -1 -1");
  814.         } else {
  815.             cout
  816.                 << 0.1 * a.p[i].x1 << ' '
  817.                 << 0.1 * a.p[i].y1 << ' '
  818.                 << 0.1 * a.p[i].x2 << ' '
  819.                 << 0.1 * a.p[i].y2 << endl;
  820.         }
  821.     }
  822.  
  823.     cerr << score(a.p) << endl;
  824. }
  825.  
  826. const double TL = 4.95;
  827.  
  828. int main() {
  829.     freopen("input.txt", "rt", stdin);
  830.     freopen("output.txt", "wt", stdout);
  831.  
  832.     int rnd = time(NULL);
  833.     cerr << rnd << endl;
  834.  
  835. //  int rnd = 33385462;
  836.  
  837.     srand(rnd);
  838.     alloc v0 = getdata();
  839.  
  840.     write(main1_ch(v0, TL));
  841.  
  842.     cerr << (double)clock() / CLOCKS_PER_SEC << endl;
  843.     return 0;
  844. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement