QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#372012 | #3313. Emergency Evacuation | PetroTarnavskyi# | TL | 0ms | 0kb | C++20 | 2.9kb | 2024-03-30 19:28:00 | 2024-03-30 19:28:01 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 1047;
int a[N][N];
int heads[2][N * N];
int sz[2];
int p[N * N];
PII c[N * N];
int n;
int mid;
int r;
void add(int x, int t)
{
heads[t][sz[t]++] = x;
}
void erase(int pos, int t)
{
sz[t]--;
swap(heads[t][pos], heads[t][sz[t]]);
}
PII next(int i)
{
auto [x, y] = c[i];
if (x == mid)
return {x, y + 1};
if (x < mid)
return {x + 1, y};
else
return {x - 1, y};
}
int findAss(int i)
{
auto [x, y] = c[i];
auto [nx, ny] = next(i);
if (nx == mid && x != mid)
return -1;
return a[nx][ny];
}
bool move(int i)
{
bool ok = false;
auto [x, y] = c[i];
auto [nx, ny] = next(i);
if (a[nx][ny] != -1)
return false;
if (ny == r)
ok = true;
if (!ok)
a[nx][ny] = i;
a[x][y] = -1;
c[i] = {nx, ny};
int idx = p[i];
if (nx == mid && x != mid)
{
p[i] = -1;
if (idx != -1)
add(idx, 1);
}
if (ok && idx != -1)
add(idx, 1);
while (idx != -1)
{
tie(nx, ny) = next(idx);
assert(nx == x && ny == y);
tie(x, y) = c[idx];
assert(a[nx][ny] == -1);
a[nx][ny] = idx;
c[idx] = {nx, ny};
a[x][y] = -1;
idx = p[idx];
}
return ok;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
FOR (i, 0, N) FOR (j, 0, N) a[i][j] = -1;
fill(p, p + N * N, -1);
int s;
cin >> r >> s >> n;
mid = s;
FOR (i, 0, n)
{
int x, y;
cin >> y >> x;
y--;
if (x <= mid)
x--;
c[i] = {x, y};
a[x][y] = i;
add(i, 0);
}
int ans = 0;
int er = -1;
while (true)
{
//if (ans > 7)
// break;
//cerr << "Iter: " << ans << '\n';
//FOR (i, 0, sz[0])
// cerr << heads[0][i] << ' ';
//cerr << '\n';
//FOR (i, 0, n)
//{
// cerr << i << ' ' << c[i].F << ' ' << c[i].S << '\n';
//}
//cerr << "------------\n";
FOR (i, 0, sz[0])
{
int h = heads[0][i];
if (h == er)
{
erase(i, 0);
i--;
continue;
}
int x = findAss(h);
if (x != -1)
{
assert(p[x] == -1);
p[x] = h;
erase(i, 0);
i--;
}
}
if (sz[0] == 0)
break;
er = -1;
FOR (i, 0, sz[0])
{
int h = heads[0][i];
auto [x, y] = c[h];
if (x == mid)
{
if (move(h))
er = h;
}
}
FOR (i, 0, sz[0])
{
int h = heads[0][i];
auto [x, y] = c[h];
if (x != mid)
{
move(h);
}
}
FOR (i, 0, sz[1])
add(heads[1][i], 0);
sz[1] = 0;
ans++;
}
cout << ans << '\n';
cerr << db(clock()) / CLOCKS_PER_SEC << '\n';
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
500 500 475000 471 382 363 533 10 249 432 511 453 185 338 745 369 837 78 8 141 531 23 175 209 819 458 506 114 909 123 347 442 193 473 300 92 652 194 153 377 372 141 608 308 540 458 90 181 522 257 743 115 752 90 58 55 803 394 478 425 588 248 231 170 326 470 463 92 881 137 48 454 599 96 413 31 1000 29...