Guest User

Untitled

a guest
Jun 16th, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #pragma comment(linker,"/STACK:300000000")
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #pragma warning(disable:4800)
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <vector>
  8. #include <string>
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <map>
  12. #include <set>
  13. #include <iomanip>
  14. #include <memory.h>
  15. #include <cstdio>
  16. #include <sstream>
  17. #include <deque>
  18. #include <bitset>
  19. #include <numeric>
  20. #include <ctime>
  21. #include <queue>
  22.  
  23. using namespace std;
  24.  
  25. #define show(x) cout << #x << " = " << (x) << endl;
  26. #define fori(i,n) for(int i = 0; i < (n); i++)
  27. #define forab(i,a,b) for(int i = (a); i <= (b); i++)
  28. #define sz(v) int((v).size())
  29. #define all(v) (v).begin(),(v).end()
  30. const double pi = 3.1415926535897932384626433832795;
  31. template<class T> T abs(const T &a) { return a >= 0 ? a : -a; };
  32. template<class T> T sqr(const T &x) { return x * x; }
  33. typedef pair<int,int> ii;
  34. typedef long long ll;
  35. ///////////////////////////////////////
  36.  
  37. int m, n, non_used;
  38.  
  39. const int size = 200500;
  40.  
  41. set<int> kupe[size];
  42. set<int> setByCnt[size];
  43. map<int,int> cnt;
  44. int where[size];
  45.  
  46. void goIn(int x, int to)
  47. {
  48.     where[x] = to;
  49.     int prevSize = sz(kupe[to]);
  50.     kupe[to].insert(x);
  51.     cnt[prevSize]--;   
  52.     if(cnt[prevSize] == 0)
  53.         cnt.erase(prevSize);
  54.     setByCnt[prevSize].erase(to);
  55.  
  56.     cnt[prevSize+1]++;
  57.     setByCnt[prevSize+1].insert(to);
  58. }
  59.  
  60. void goOut(int x)
  61. {
  62.     int from = where[x];
  63.     int prevSize = sz(kupe[from]);
  64.     kupe[from].erase(x);
  65.     cnt[prevSize]--;   
  66.     if(cnt[prevSize] == 0)
  67.         cnt.erase(prevSize);
  68.     setByCnt[prevSize].erase(from);
  69.  
  70.     cnt[prevSize-1]++;
  71.     setByCnt[prevSize-1].insert(from);
  72. }
  73.  
  74. void add(int x)
  75. {
  76.     int mi = cnt.begin()->first;
  77.     int to = *setByCnt[mi].begin();
  78.     goIn(x,to);
  79. }
  80.  
  81. int getNearest(set<int> &setBySize, int ind)
  82. {
  83.     set<int>::iterator it = setBySize.lower_bound(ind);
  84.     if(it == setBySize.end())
  85.         it--;
  86.     int ans = *it;
  87.     if(it != setBySize.begin())
  88.     {
  89.         it--;
  90.         if(abs(*it-ind) <= ans-ind)
  91.             ans = *it;
  92.     }
  93.     return ans;
  94. }
  95.  
  96. void remove(int x)
  97. {
  98.     int w = where[x];
  99.     goOut(x);
  100.     int ma = (--cnt.end())->first;
  101.     if(sz(kupe[w]) == ma-2)
  102.     {
  103.         int from = getNearest(setByCnt[ma],w);
  104.         int x = *kupe[from].begin();
  105.         goOut(x);
  106.         goIn(x,w);
  107.     }
  108. }
  109.  
  110. int main()
  111. {
  112.     if(ifstream("input.txt"))
  113.     {
  114.         freopen("input.txt","r",stdin);
  115.         freopen("output.txt","w",stdout);
  116.     }
  117.     cin >> m >> n >> non_used;
  118.     fori(i,n)
  119.         setByCnt[0].insert(i);
  120.     cnt[0] = n;
  121.  
  122.     int plusCnt = 0;
  123.     fori(i,m)
  124.     {
  125.         char ch;
  126.         cin >> ch;
  127.         if(ch == '+')
  128.         {
  129.             add(plusCnt);
  130.             plusCnt++;
  131.         }
  132.         else
  133.         {
  134.             int x;         
  135.             cin >> x;
  136.             x--;
  137.             remove(x);
  138.         }
  139.     }
  140.     fori(i,n)
  141.     {
  142.         cout << sz(kupe[i]) << ' ';
  143.         for(set<int>::iterator it = kupe[i].begin(); it != kupe[i].end(); it++)
  144.             cout << (*it)+1 << ' ';
  145.         cout << endl;
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment