[存档]Young Tableau
you know...
#include <cassert> #include <algorithm> using namespace std; const int INF = 1111111111; const int M = 1111; const int N = 1111; //minimum young_tableau struct young_tableau { int a[M][N]; young_tableau() { for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) a[i][j] = INF; } bool empty() { return !(a[0][0]<INF); } bool full() { return a[M-1][N-1]<INF; } //O(1) int top() { assert(!empty()); return a[0][0]; } void sink(int i, int j) { int mi = i, mj = j; if (i<M-1 && a[i+1][j]<a[mi][mj]) mi = i+1, mj = j; if (j<N-1 && a[i][j+1]<a[mi][mj]) mi = i, mj = j+1; if (mi!=i || mj!=j) { swap(a[i][j], a[mi][mj]); sink(mi, mj); } } //O(M+N) void pop() { assert(!empty()); a[0][0] = INF; sink(0, 0); } void rise(int i, int j) { int mi = i, mj = j; if (i>0 && a[mi][mj]<a[i-1][j]) mi = i-1, mj = j; if (j>0 && a[mi][mj]<a[i][j-1]) mi = i, mj = j-1; if (mi!=i || mj!=j) { swap(a[i][j], a[mi][mj]); rise(mi, mj); } } //O(M+N) void push(int key) { assert(!full()); a[M-1][N-1] = key; rise(M-1, N-1); } //O(M+N) //(M, N) for not found pair<int, int> lower_bound(int key) { pair<int, int> res = make_pair(M, N); int i = 0, j = N-1; while (i<M && j>=0) { if (a[i][j]<key) i++; else res = make_pair(i, j--); } return i<M&&j>=0 ? res : make_pair(M, N); } pair<int, int> find(int key) { pair<int, int> res = lower_bound(key); if (res==make_pair(M, N) || key<a[res.first][res.second]) return make_pair(M, N); else return res; } };