Untitled
unknown
plain_text
a year ago
2.2 kB
4
Indexable
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <cstring>
using namespace std;
struct Theater{
int seat[101];
int slot;
} theater[2001];
struct cmp{
bool operator() (int a, int b) {
return a > b;
}
};
int n;
int dir[4] = {-10, 1, 10, -1};
unordered_map<int, pair<int, int>> mp;
struct Result {
int id;
int num;
};
void init(int N)
{
n = N;
mp.clear();
for(int i=1; i<=n; i++) {
theater[i].slot = 100;
for(int j = 1; j<=100; j++) {
theater[i].seat[j] = 0;
}
}
}
bool bfs(int tId, int seat, int mId, int k) {
int cnt = 0;
int visit[101];
memset(visit, 0, sizeof(visit));
priority_queue<int, vector<int>, cmp> pq;
pq.push(seat);
visit[seat] = 1;
while(!pq.empty()) {
int curSeat= pq.top();
pq.pop();
cnt++;
if(cnt == k) break;
visit[curSeat] = 1;
for(int k = 0; k<4; k++) {
int nextSeat = curSeat + dir[k];
if(nextSeat > 0 && nextSeat < 101) {
if(!visit[nextSeat] && !theater[tId].seat[nextSeat]) {
pq.push(nextSeat);
visit[nextSeat] = 1;
}
}
}
}
if(cnt == k) {
for(int i=1; i<101; i++) {
if(visit[i] == 1) {
theater[tId].seat[i] = mId;
}
}
return true;
}
return false;
}
Result reserveSeats(int mID, int K)
{
Result res;
for(int tId=1; tId<=n; tId++) {
if(theater[tId].slot >= K) {
for(int seatt=1; seatt<=100; seatt++) {
if(theater[tId].seat[seatt] == 0 && bfs(tId, seatt, mID, K)) {
pair<int, int> infor;
infor.first = tId;
infor.second = seatt;
mp[mID] = infor;
theater[tId].slot -= K;
res.id = tId;
res.num = seatt;
return res;
}
}
}
}
res.id = 0;
res.num = 0;
return res;
}
Result cancelReservation(int mID)
{
Result res;
int cnt = 0;
int sum = 0;
pair<int, int> infor;
infor = mp[mID];
int tId = infor.first;
int seat = infor.second;
for(int i=seat; i<101; i++) {
if(theater[tId].seat[i] == mID) {
cnt++;
sum+=i;
theater[tId].seat[i] = 0;
}
}
mp.erase(mID);
theater[tId].slot += cnt;
res.id = tId;
res.num = 0;
return res;
}Editor is loading...
Leave a Comment