Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker,"/STACK:300000000")
- #define _CRT_SECURE_NO_WARNINGS
- #pragma warning(disable:4800)
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <set>
- #include <iomanip>
- #include <memory.h>
- #include <cstdio>
- #include <sstream>
- #include <deque>
- #include <bitset>
- #include <numeric>
- #include <ctime>
- #include <queue>
- using namespace std;
- #define show(x) cout << #x << " = " << (x) << endl;
- #define fori(i,n) for(int i = 0; i < (n); i++)
- #define forab(i,a,b) for(int i = (a); i <= (b); i++)
- #define sz(v) int((v).size())
- #define all(v) (v).begin(),(v).end()
- const double pi = 3.1415926535897932384626433832795;
- template<class T> T abs(const T &a) { return a >= 0 ? a : -a; };
- template<class T> T sqr(const T &x) { return x * x; }
- typedef pair<int,int> ii;
- typedef long long ll;
- ///////////////////////////////////////
- int m, n, non_used;
- const int size = 200500;
- set<int> kupe[size];
- set<int> setByCnt[size];
- map<int,int> cnt;
- int where[size];
- void goIn(int x, int to)
- {
- where[x] = to;
- int prevSize = sz(kupe[to]);
- kupe[to].insert(x);
- cnt[prevSize]--;
- if(cnt[prevSize] == 0)
- cnt.erase(prevSize);
- setByCnt[prevSize].erase(to);
- cnt[prevSize+1]++;
- setByCnt[prevSize+1].insert(to);
- }
- void goOut(int x)
- {
- int from = where[x];
- int prevSize = sz(kupe[from]);
- kupe[from].erase(x);
- cnt[prevSize]--;
- if(cnt[prevSize] == 0)
- cnt.erase(prevSize);
- setByCnt[prevSize].erase(from);
- cnt[prevSize-1]++;
- setByCnt[prevSize-1].insert(from);
- }
- void add(int x)
- {
- int mi = cnt.begin()->first;
- int to = *setByCnt[mi].begin();
- goIn(x,to);
- }
- int getNearest(set<int> &setBySize, int ind)
- {
- set<int>::iterator it = setBySize.lower_bound(ind);
- if(it == setBySize.end())
- it--;
- int ans = *it;
- if(it != setBySize.begin())
- {
- it--;
- if(abs(*it-ind) <= ans-ind)
- ans = *it;
- }
- return ans;
- }
- void remove(int x)
- {
- int w = where[x];
- goOut(x);
- int ma = (--cnt.end())->first;
- if(sz(kupe[w]) == ma-2)
- {
- int from = getNearest(setByCnt[ma],w);
- int x = *kupe[from].begin();
- goOut(x);
- goIn(x,w);
- }
- }
- int main()
- {
- if(ifstream("input.txt"))
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- }
- cin >> m >> n >> non_used;
- fori(i,n)
- setByCnt[0].insert(i);
- cnt[0] = n;
- int plusCnt = 0;
- fori(i,m)
- {
- char ch;
- cin >> ch;
- if(ch == '+')
- {
- add(plusCnt);
- plusCnt++;
- }
- else
- {
- int x;
- cin >> x;
- x--;
- remove(x);
- }
- }
- fori(i,n)
- {
- cout << sz(kupe[i]) << ' ';
- for(set<int>::iterator it = kupe[i].begin(); it != kupe[i].end(); it++)
- cout << (*it)+1 << ' ';
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment