Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. //Daniel Grzegorzewski
  2. #include <bits/stdc++.h>
  3.  
  4. #define MP make_pair
  5. #define PB push_back
  6. #define ST first
  7. #define ND second
  8.  
  9. using namespace std;
  10.  
  11. typedef pair<int, int> PII;
  12. typedef vector<int> VI;
  13. typedef vector<PII> VII;
  14. typedef long long LL;
  15.  
  16. void init_ios()
  17. {
  18.      ios_base::sync_with_stdio(0);
  19.      cin.tie(0);
  20. }
  21.  
  22. const int N = (int)1e6 + 10;
  23.  
  24. bool flaga;
  25.  
  26. struct AVLnode
  27. {
  28.     AVLnode *left;
  29.     AVLnode *right;
  30.     AVLnode *parent;
  31.     int key, size, bf;
  32.  
  33.     void create(int value)
  34.     {
  35.         left = NULL;
  36.         right = NULL;
  37.         parent = NULL;
  38.         key = value;
  39.         size = 1;
  40.         bf = 0;
  41.     }
  42. };
  43.  
  44. struct AVLtree
  45. {
  46.     AVLnode *root;
  47.  
  48.     void init()
  49.     {
  50.         root = NULL;
  51.     }
  52.  
  53.     void LL(AVLnode *A)
  54.     {
  55.         AVLnode *B = A->left;
  56.         AVLnode *p = A->parent;
  57.  
  58.         A->size = 1;
  59.         if (B->right != NULL)
  60.             A->size += B->right->size;
  61.         if (A->right != NULL)
  62.             A->size += A->right->size;
  63.  
  64.         B->size = 1+A->size;
  65.         if (B->left != NULL)
  66.             B->size += B->left->size;
  67.  
  68.         A->left = B->right;
  69.         if (A->left != NULL)
  70.             A->left->parent = A;
  71.         B->right = A;
  72.         B->parent = p;
  73.         A->parent = B;
  74.  
  75.         if (p == NULL)
  76.             root = B;
  77.         else if (p->left == A)
  78.             p->left = B;
  79.         else
  80.             p->right = B;
  81.         if (B->bf == 1) {
  82.             A->bf = 0;
  83.             B->bf = 0;
  84.         }
  85.         else {
  86.             A->bf = 1;
  87.             B->bf = -1;
  88.         }
  89.     }
  90.  
  91.     void RR(AVLnode *A)
  92.     {
  93.         AVLnode *B = A->right;
  94.         AVLnode *p = A->parent;
  95.  
  96.         A->size = 1;
  97.         if (A->left != NULL)
  98.             A->size += A->left->size;
  99.         if (B->left != NULL)
  100.             A->size += B->left->size;
  101.  
  102.         B->size = 1;
  103.         B->size += A->size;
  104.         if (B->right != NULL)
  105.             B->size += B->right->size;
  106.  
  107.         A->right = B->left;
  108.         if (A->right != NULL)
  109.             A->right->parent = A;
  110.         B->left = A;
  111.         B->parent = p;
  112.         A->parent = B;
  113.  
  114.         if (p == NULL)
  115.             root = B;
  116.         else if (p->left == A)
  117.             p->left = B;
  118.         else
  119.             p->right = B;
  120.         if (B->bf == -1) {
  121.             A->bf = 0;
  122.             B->bf = 0;
  123.         }
  124.         else {
  125.             A->bf = -1;
  126.             B->bf = 1;
  127.         }
  128.     }
  129.  
  130.     void LR(AVLnode *A)
  131.     {
  132.         AVLnode *B = A->left;
  133.         AVLnode *C = B->right;
  134.         AVLnode *p = A->parent;
  135.  
  136.         A->size = 1;
  137.         if (A->right != NULL)
  138.             A->size += A->right->size;
  139.         if (C->right != NULL)
  140.             A->size += C->right->size;
  141.         B->size = 1;
  142.         if (B->left != NULL)
  143.             B->size += B->left->size;
  144.         if (C->left != NULL)
  145.             B->size += C->left->size;
  146.  
  147.         C->size = A->size + B->size + 1;
  148.  
  149.         B->right = C->left;
  150.         if (B->right != NULL)
  151.             B->right->parent = B;
  152.         A->left = C->right;
  153.         if (A->left != NULL)
  154.             A->left->parent = A;
  155.  
  156.         C->right = A;
  157.         C->left = B;
  158.         A->parent = B->parent = C;
  159.         C->parent = p;
  160.  
  161.         if (p != NULL) {
  162.             if (p->left == A)
  163.                 p->left = C;
  164.             else
  165.                 p->right = C;
  166.         }
  167.         else
  168.             root = C;
  169.  
  170.         if (C->bf == 1)
  171.             A->bf = -1;
  172.         else
  173.             A->bf = 0;
  174.         if (C->bf == -1)
  175.             B->bf = 1;
  176.         else
  177.             B->bf = 0;
  178.  
  179.         C->bf = 0;
  180.     }
  181.  
  182.     void RL(AVLnode *A)
  183.     {
  184.         AVLnode *B = A->right;
  185.         AVLnode *C = B->left;
  186.         AVLnode *p = A->parent;
  187.  
  188.         A->size = 1;
  189.         if (A->left != NULL)
  190.             A->size += A->left->size;
  191.         if (C->left != NULL)
  192.             A->size += C->left->size;
  193.  
  194.         B->size = 1;
  195.         if (B->right != NULL)
  196.             B->size += B->right->size;
  197.         if (C->right != NULL)
  198.             B->size += C->right->size;
  199.  
  200.         C->size = A->size + B->size + 1;
  201.  
  202.         B->left = C->right;
  203.         if (B->left != NULL)
  204.             B->left->parent = B;
  205.         A->right = C->left;
  206.         if (A->right != NULL)
  207.             A->right->parent = A;
  208.  
  209.         C->left = A;
  210.         C->right = B;
  211.         A->parent = B->parent = C;
  212.         C->parent = p;
  213.  
  214.         if (p != NULL) {
  215.             if (p->left == A)
  216.                 p->left = C;
  217.             else
  218.                 p->right = C;
  219.         }
  220.         else
  221.             root = C;
  222.  
  223.         if (C->bf == -1)
  224.             A->bf = 1;
  225.         else
  226.             A->bf = 0;
  227.         if (C->bf == 1)
  228.             B->bf = -1;
  229.         else
  230.             B->bf = 0;
  231.  
  232.         C->bf = 0;
  233.     }
  234.  
  235.     void insert(int value)
  236.     {
  237.         AVLnode *w = new AVLnode;
  238.         AVLnode *p;
  239.         AVLnode *r;
  240.         AVLnode *pom;
  241.         bool flag = false;
  242.         w->create(value);
  243.        
  244.         if (root == NULL) {
  245.             root = w;
  246.             return;
  247.         }
  248.         p = root;
  249.  
  250.         while (true) {
  251.             if (value <= p->key) {
  252.                 if (p->left == NULL) {
  253.                     p->left = w;
  254.                     break;
  255.                 }
  256.                 p = p->left;
  257.             }
  258.             else {
  259.                 if (p->right == NULL) {
  260.                     p->right = w;
  261.                     break;
  262.                 }
  263.                 p = p->right;
  264.             }
  265.         }
  266.         w->parent = p;
  267.         pom = p;
  268.         while (pom != NULL) {
  269.             pom->size++;
  270.             pom = pom->parent;
  271.         }
  272.  
  273.         if (p->bf != 0) {
  274.             p->bf = 0;
  275.             return;
  276.         }
  277.         if (p->left == w)
  278.             p->bf = 1;
  279.         else
  280.             p->bf = -1;
  281.         r = p->parent;
  282.         while (r) {
  283.             if (r->bf != 0) {
  284.                 flag = true;
  285.                 break;
  286.             }
  287.             if (r->left == p)
  288.                 r->bf = 1;
  289.             else
  290.                 r->bf = -1;
  291.             p = r;
  292.             r = r->parent;
  293.         }
  294.         if (!flag)
  295.             return;
  296.         if (r->bf == 1) {
  297.             if (r->right == p)
  298.                 r->bf = 0;
  299.             else if (p->bf == -1)
  300.                 LR(r);
  301.             else
  302.                 LL(r);
  303.         }
  304.         else {
  305.             if (r->left == p)
  306.                 r->bf = 0;
  307.             else if (p->bf == 1)
  308.                 RL(r);
  309.             else
  310.                 RR(r);
  311.         }
  312.     }
  313. };
  314.  
  315. void Union(AVLtree &big, AVLnode *sma) //do drzewa big wrzucam drzewo o korzeniu w sma
  316. {
  317.     if (sma == NULL)
  318.         return;
  319.     if (sma->left != NULL)
  320.         Union(big, sma->left);
  321.     if (sma->right != NULL)
  322.         Union(big, sma->right);
  323.     big.insert(sma->key);
  324.     delete sma;
  325. }
  326.  
  327. int kta(AVLnode *w, int k) //k'ta co do wielkosci liczba w drzewie
  328. {
  329.     if (k > w->size)
  330.         return 0;
  331.     if (w->left == NULL && k == 1)
  332.         return w->key;
  333.     else if (w->left == NULL)
  334.         return kta(w->right, k-1);
  335.     if (w->left->size == k-1)
  336.         return w->key;
  337.     if (w->left->size > k-1)
  338.         return kta(w->left, k);
  339.     else
  340.         return kta(w->right, k-1-w->left->size);
  341. }
  342.  
  343. double mediana(AVLnode *w)
  344. {
  345.     if (w == NULL)
  346.         return 0;
  347.     int n = w->size;
  348.     if (n&1)
  349.         return (double)kta(w, (n+1)/2);
  350.     double res = (double)kta(w, n/2);
  351.     res += (double)kta(w, n/2+1);
  352.     res /= 2;
  353.     return res;
  354. }
  355.  
  356. int n, a[N];
  357. double res;
  358. vector<double> out;
  359. list<AVLtree> l;
  360. AVLtree tree;
  361.  
  362. int main()
  363. {
  364.     init_ios();
  365.     cout<<fixed<<setprecision(4);
  366.     cin >> n;
  367.     for (int i = 1; i <= n; ++i) {
  368.         cin >> a[i];
  369.         tree.init();
  370.         tree.insert(a[i]);
  371.         l.PB(tree);
  372.     }
  373.     for (auto it = l.begin(); it != l.end();) {
  374.         auto tmp = it;
  375.         ++tmp;
  376.         if (tmp == l.end())
  377.             break;
  378.         if (mediana(it->root) > mediana(tmp->root)) {
  379.             if (flaga)
  380.                 return 0;
  381.             if (it->root->size <= tmp->root->size) {
  382.                 auto rem = it;
  383.                 if (rem != l.begin())
  384.                     --rem;
  385.                 else
  386.                     ++rem;
  387.                 Union(*tmp, it->root);
  388.                 l.erase(it);
  389.                 it = rem;
  390.             }
  391.             else {
  392.                 Union(*it, tmp->root);
  393.                 l.erase(tmp);
  394.                 if (it != l.begin())
  395.                     --it;
  396.             }
  397.         }
  398.         else
  399.             ++it;
  400.     }
  401.     int t = 1;
  402.     for (auto it = l.begin(); it != l.end(); ++it) {
  403.         double med = mediana(it->root);
  404.         if (flaga)
  405.             return 0;
  406.         for (int i = 1; i <= it->root->size; ++i) {
  407.             out.PB(med);
  408.             res += abs(med-a[t++]);
  409.         }
  410.     }
  411.     cout<<res<<"\n";
  412.     for (int i = 0; i < out.size(); ++i)
  413.         cout<<out[i]<<"\n";
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement