Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.2 kB
3
Indexable
#include <iostream>
using namespace std;
int n, m, map[20001][1001], empty[20001], id[50001];
int curRow, cursor[20001], rowStore[50001], colStore[50001];
bool checkEmpty;
void init(int N, int M)
{
	n = N;
	m = M;
	for(int i=0; i<=50000; i++) {
		id[i] = 0;
	}
	for(int i=0; i<N; i++) {
		for(int j=0; j<M; j++) {
			map[i][j] = 0;	
		}
	}
		
	for(int i=0; i<N; i++) {
		rowStore[i] = -1;
		empty[i] = M;
		cursor[i] = 0;
	}
	for(int i=0; i<M; i++)
		colStore[i] = -1;
}

int writeWord(int mId, int mLen)
{
	curRow = mLen;
	checkEmpty = false;
	for(int i=0; i<n; i++) {
		if(empty[i] >= curRow) {
			 curRow = i;
			 rowStore[mId] = curRow;
			 checkEmpty = true;
			 break;
		}
	}
	if(!checkEmpty) return -1;
	colStore[mId] = cursor[curRow];
	for(int i=cursor[curRow]; i<mLen + cursor[curRow]; i++) {
		map[curRow][i] = mId;
	}
	id[mId] = 1;
	cursor[curRow] += mLen;
	empty[curRow] -= mLen;
    return curRow;
}

int eraseWord(int mId)
{
	if(id[mId] == 0)
		return -1;
	for(int i=colStore[mId]; i<=m; i++) {
		if(map[rowStore[mId]][i] == mId) {
			map[rowStore[mId]][i] = 0;
			empty[mId]++;
		}
		else break;
	}
	return rowStore[mId];
}
Leave a Comment