QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#405283 | #437. Fun Tour | Max_s_xaM | 10 | 220ms | 50616kb | C++20 | 4.0kb | 2024-05-05 16:23:33 | 2024-05-05 16:23:34 |
Judging History
answer
#include "fun.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
typedef long long ll;
typedef double lf;
using namespace std;
const int MAXN = 1e5 + 10;
int n, q;
vector <int> res, ans;
map <pair <int, int>, int> dist, tsz;
inline int GetDist(int u, int v)
{
if (dist[make_pair(u, v)]) return dist[make_pair(u, v)];
return dist[make_pair(u, v)] = dist[make_pair(v, u)] = hoursRequired(u - 1, v - 1);
}
inline int GetSize(int u, int v)
{
if (tsz[make_pair(u, v)]) return tsz[make_pair(u, v)];
return tsz[make_pair(u, v)] = attractionsBehind(u - 1, v - 1);
}
int rt, sz[MAXN], dep[MAXN], bel[MAXN];
vector <int> son, vec[3];
inline void Solve2()
{
if (vec[0].size() < vec[1].size()) swap(vec[0], vec[1]);
for (int i = vec[0].size() - 1, j = vec[1].size() - 1; ~i; i--)
{
res.push_back(vec[0][i]);
if (j == -1) res.push_back(rt);
else res.push_back(vec[1][j--]);
}
if (res.size() != n) res.push_back(rt);
}
vector <int> createFunTour(int N, int Q)
{
n = N, q = Q;
if (n == 2)
{
res.push_back(0), res.push_back(1);
return res;
}
sz[1] = n;
for (int i = 2; i <= n; i++) sz[i] = GetSize(1, i);
int mn = n; rt = 1;
for (int i = 1; i <= n; i++)
if (n - sz[i] <= n / 2 && mn > sz[i]) mn = sz[i], rt = i;
for (int i = 1; i <= n; i++)
if (i != rt)
{
dep[i] = GetDist(rt, i);
if (dep[i] == 1) son.push_back(i);
}
bel[rt] = -1;
for (int i = 1; i <= n; i++)
if (dep[i] == 1)
{
for (int j = 0; j < son.size(); j++)
if (son[j] == i) bel[i] = j;
}
else if (dep[i] > 1)
{
bel[i] = -1;
for (int j = 0; j < son.size() - 1; j++)
if (GetDist(i, son[j]) == dep[i] - 1) {bel[i] = j; break;}
if (bel[i] == -1) bel[i] = son.size() - 1;
}
for (int i = 1; i <= n; i++)
if (dep[i]) vec[bel[i]].push_back(i);
for (int i = 0; i < son.size(); i++) sort(vec[i].begin(), vec[i].end(), [&](int x, int y) {return dep[x] < dep[y];});
// cout << rt << '\n';
// for (int i = 1; i <= n; i++) cout << bel[i] << ' '; cout << '\n';
// for (int i = 1; i <= n; i++) cout << dep[i] << ' '; cout << '\n';
if (son.size() == 2)
{
Solve2();
for (auto x : res) ans.push_back(x - 1);
// for (auto x : res) cout << x << " "; cout << '\n';
return ans;
}
int num = n - 1, mxs; int pre = -1;
while ((mxs = max(vec[0].size(), max(vec[1].size(), vec[2].size()))) < num - mxs)
{
int fr = 0, cur = 0;
for (int i = 0; i < 3; i++)
if (i != pre && vec[i].size() && fr < vec[i].back()) fr = vec[i].back(), cur = i;
res.push_back(vec[cur].back()), vec[cur].pop_back(), num--;
pre = cur;
}
// cout << res.size() << "\n";
if (vec[0].size() < vec[1].size()) swap(vec[0], vec[1]);
if (vec[0].size() < vec[2].size()) swap(vec[0], vec[2]);
for (auto x : vec[2]) vec[1].push_back(x);
sort(vec[1].begin(), vec[1].end(), [&](int x, int y) {return dep[x] < dep[y];});
// for (auto x : vec[0]) cout << x << ' '; cout << '\n';
// for (auto x : vec[1]) cout << x << ' '; cout << '\n';
if (vec[1].size() == 0)
{
res.push_back(vec[0].back()), res.push_back(rt);
for (auto x : res) ans.push_back(x - 1);
// for (auto x : res) cout << x << " "; cout << '\n';
return ans;
}
int fst1 = vec[0].back(), fst2 = vec[1].back();
if (res.size() && (bel[fst1] == bel[res.back()] || (res.size() >= 2 && dep[res[res.size() - 2]] < dep[fst1])))
{
swap(vec[0], vec[1]);
Solve2();
if (res.size() != n) res.push_back(vec[1].front());
}
else Solve2();
for (auto x : res) ans.push_back(x - 1);
// for (auto x : res) cout << x << " "; cout << '\n';
return ans;
}
詳細信息
Subtask #1:
score: 0
Accepted
Test #1:
score: 0
Accepted
time: 1ms
memory: 3756kb
input:
2 400000 1 0
output:
0 1
result:
ok correct
Subtask #2:
score: 0
Accepted
Test #3:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
4 400000 1 0 2 0 3 1
output:
2 3 0 1
result:
ok correct
Subtask #3:
score: 0
Accepted
Test #9:
score: 0
Accepted
time: 0ms
memory: 4020kb
input:
4 400000 1 0 2 1 3 2
output:
0 3 1 2
result:
ok correct
Subtask #4:
score: 0
Accepted
Test #13:
score: 0
Accepted
time: 1ms
memory: 4020kb
input:
4 400000 0 1 0 2 0 3
output:
3 1 2 0
result:
ok correct
Subtask #5:
score: 0
Accepted
Test #20:
score: 0
Accepted
time: 0ms
memory: 3768kb
input:
18 400000 1 0 2 0 3 1 4 1 5 2 6 2 7 3 8 3 9 4 10 4 11 5 12 5 13 6 14 6 15 7 16 7 17 8
output:
17 14 16 13 15 12 10 11 9 6 8 5 7 2 4 0 3 1
result:
ok correct
Subtask #6:
score: 0
Wrong Answer
Test #27:
score: 0
Wrong Answer
time: 1ms
memory: 3800kb
input:
18 400000 7 6 8 7 5 8 4 5 3 4 9 3 10 11 12 10 13 12 14 13 9 14 2 9 15 2 16 15 17 16 1 17 0 1
output:
WA: Tour is not fun
result:
wrong output format Expected integer, but "WA:" found
Subtask #7:
score: 0
Accepted
Test #33:
score: 0
Accepted
time: 1ms
memory: 4072kb
input:
500 400000 33 414 398 33 254 398 400 254 376 400 302 376 466 302 362 466 301 362 267 301 417 267 413 417 14 413 305 14 353 305 83 353 227 83 57 227 446 57 406 446 154 406 336 154 195 336 405 195 264 405 76 264 203 76 459 203 178 459 72 178 296 72 488 296 404 488 60 404 156 60 393 156 108 393 216 108...
output:
212 414 88 33 124 398 399 254 190 400 10 376 366 302 234 466 432 362 28 301 188 267 237 417 64 413 268 14 39 305 349 353 345 83 125 227 70 57 276 446 420 406 369 154 48 336 211 195 482 405 8 264 22 76 487 203 9 459 230 178 339 72 95 296 340 488 50 404 102 60 174 156 47 393 461 108 424 216 181 106 13...
result:
ok correct
Subtask #8:
score: 0
Wrong Answer
Test #41:
score: 0
Wrong Answer
time: 1ms
memory: 3952kb
input:
501 400000 1 0 2 0 3 1 4 1 5 2 6 2 7 3 8 3 9 4 10 4 11 5 12 5 13 6 14 6 15 7 16 7 17 8 18 8 19 9 20 9 21 10 22 10 23 11 24 11 25 12 26 12 27 13 28 13 29 14 30 14 31 15 32 15 33 16 34 16 35 17 36 17 37 18 38 18 39 19 40 19 41 20 42 20 43 21 44 21 45 22 46 22 47 23 48 23 49 24 50 24 51 25 52 25 53 26 ...
output:
WA: Tour is not fun
result:
wrong output format Expected integer, but "WA:" found
Subtask #9:
score: 0
Wrong Answer
Test #51:
score: 0
Wrong Answer
time: 2ms
memory: 4328kb
input:
1000 400000 6 5 7 8 6 7 9 6 11 10 11 12 13 11 14 15 13 14 9 13 16 9 18 17 18 19 20 18 22 21 22 23 20 22 24 20 26 25 26 27 28 26 30 29 30 31 28 30 24 28 16 24 32 16 33 32 35 34 35 36 37 35 39 38 39 40 37 39 41 37 43 42 43 44 45 43 47 46 47 48 45 47 41 45 49 41 50 51 52 50 54 53 54 55 52 54 56 52 58 5...
output:
WA: Tour is not fun
result:
wrong output format Expected integer, but "WA:" found
Subtask #10:
score: 0
Accepted
Test #59:
score: 0
Accepted
time: 220ms
memory: 50616kb
input:
99999 400000 11681 58292 11681 63929 49752 11681 30596 74400 30596 39261 49752 30596 19390 49752 89694 31923 19390 89694 54297 19390 42389 12902 42389 60328 72803 42389 69881 43761 69881 95741 72803 69881 96271 72803 63872 20658 63872 93588 35833 63872 48418 44153 35833 48418 96271 35833 54297 96271...
output:
62996 54185 26009 13388 62998 54193 26002 54198 26001 54199 63008 54200 25999 54204 63016 54210 63017 54212 25997 13385 25996 54217 73666 54218 63031 54226 63033 69843 63036 54232 25989 54238 63040 54243 25986 54244 25985 54247 63062 54250 63064 54252 63065 54253 63066 54255 25968 54258 63030 13383 ...
result:
ok correct
Subtask #11:
score: 10
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Subtask #12:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
0%
Subtask #13:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #8:
0%
Subtask #14:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #6:
0%
Subtask #15:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
0%