# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1097337 |
2024-10-07T01:20:22 Z |
gyg |
Parking (CEOI22_parking) |
C++17 |
|
2000 ms |
7524 KB |
#include <bits/stdc++.h>
using namespace std;
#define arr array
#define vct vector
#define pii pair<int, int>
#define mpii make_pair
#define fir first
#define sec second
#define hset unordered_set
const int N = 2e5 + 5, M = 2e5 + 5;
int n, m;
arr<arr<int, 2>, M> vl;
arr<pii, N> ps;
int fr(int i) {
assert(i != 0);
return ((i <= n) ? i + n : i - n);
}
void prcmp() {
hset<int> vls;
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= 1; j++) {
if (vl[i][j] == 0) continue;
if (!vls.count(vl[i][j])) vls.insert(vl[i][j]);
else vl[i][j] += n;
ps[vl[i][j]] = {i, j};
}
}
}
vct<pii> ans;
void mv(int i, int j) {
assert(i != j);
ans.push_back({i, j});
int k = (vl[i][1] != 0) ? vl[i][1] : vl[i][0];
vl[ps[k].fir][ps[k].sec] = 0;
ps[k] = (vl[j][0] == 0) ? mpii(j, 0) : mpii(j, 1);
vl[ps[k].fir][ps[k].sec] = k;
}
bool fn() {
for (int i = 1; i <= n; i++)
if (ps[i].fir != ps[fr(i)].fir) return false;
return true;
}
void cmp() {
// for (int i = 1; i <= m; i++) {
// cout << vl[i][0] << " " << vl[i][1] << endl;
// }
while (true) {
if (fn()) break;
bool flg = false;
for (int i = 1; i <= n; i++) {
int j = ps[i].fir;
int k = fr(i);
if (vl[j][1] == 0 && (ps[k].sec == 1 || vl[ps[k].fir][1] == 0)) {
mv(ps[k].fir, j);
flg = true; break;
}
}
if (flg) continue;
int emp = 0;
for (int i = 1; i <= m; i++)
if (vl[i][0] == 0) { emp = i; break; }
if (emp == 0) { cout << -1 << endl; exit(0); }
for (int i = 1; i <= n; i++) {
if (ps[i].sec == 1 && ps[fr(i)].sec == 1) {
mv(ps[i].fir, emp);
flg = true; break;
}
}
if (flg) continue;
for (int i = 1; i <= n; i++) {
if (ps[i].sec == 1 && ps[i].fir != ps[fr(i)].fir) {
mv(ps[i].fir, emp);
break;
}
}
}
// for (int i = 1; i <= m; i++) {
// cout << vl[i][0] << " " << vl[i][1] << endl;
// }
}
int main() {
// freopen("prk.in", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= m; i++)
for (int j = 0; j <= 1; j++)
cin >> vl[i][j];
prcmp();
cmp();
cout << ans.size() << endl;
for (pii x : ans) cout << x.fir << " " << x.sec << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2071 ms |
7524 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |