Untitled
unknown
plain_text
a year ago
1.9 kB
11
Indexable
#include <iostream>
#include <set>
using namespace std;
const int MAX_ID = 50001;
int n, m, id[MAX_ID];
int rowStore[MAX_ID], colStore[MAX_ID];
set<pair<int, int>> availableRows; // {empty slots, row index}
set<pair<int, int>> occupiedCells[MAX_ID]; // {column index, row index}
void init(int N, int M) {
n = N;
m = M;
availableRows.clear();
for(int i = 0; i < n; i++) {
availableRows.insert({M, i});
}
for(int i = 0; i < MAX_ID; i++) {
id[i] = 0;
rowStore[i] = -1;
colStore[i] = -1;
occupiedCells[i].clear();
}
}
int writeWord(int mId, int mLen) {
auto it = availableRows.lower_bound({mLen, 0});
if (it == availableRows.end()) {
return -1; // Không có hàng nào đủ chỗ trống
}
int curRow = it->second;
int emptySlots = it->first;
// Xóa hàng này khỏi tập hợp vì chúng ta sẽ cập nhật lại số chỗ trống
availableRows.erase(it);
colStore[mId] = m - emptySlots; // Bắt đầu từ vị trí trống đầu tiên
rowStore[mId] = curRow;
for (int i = 0; i < mLen; i++) {
occupiedCells[mId].insert({colStore[mId] + i, curRow});
}
// Cập nhật lại số chỗ trống trong hàng và đưa nó lại vào tập hợp
availableRows.insert({emptySlots - mLen, curRow});
id[mId] = 1;
return curRow;
}
int eraseWord(int mId) {
if (id[mId] == 0) {
return -1;
}
int row = rowStore[mId];
int len = occupiedCells[mId].size();
// Xóa các ô đã được điền từ
for (auto& cell : occupiedCells[mId]) {
availableRows.erase({m - (int)occupiedCells[mId].size(), cell.second});
}
availableRows.insert({m - (m - (int)occupiedCells[mId].size() + len), row});
occupiedCells[mId].clear();
id[mId] = 0;
return row;
}
Editor is loading...
Leave a Comment