QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#346779 | #3858. Systematic salesman | Kirill22# | WA | 392ms | 4132kb | C++23 | 4.7kb | 2024-03-08 23:15:25 | 2024-03-08 23:15:25 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define ld long double
long double kek(const pair<int, int>& x, const pair<int, int>& y) {
return sqrtl((long double) (x.first - y.first) * (x.first - y.first) +
(x.second - y.second) * (x.second - y.second));
}
bool equal(long double x, long double y) {
return abs(x - y) < 1e-9;
}
vector<ld> solve(vector<pair<int, int>> a, vector<ld> dp) {
const int n = (int) a.size();
if (n == 1) {
return dp;
}
map<pair<int, int>, int> id;
for (int i = 0; i < n; i++) {
id[a[i]] = i;
}
std::sort(a.begin(), a.end());
const int m = n / 2;
vector<pair<int, int>> L, R;
vector<ld> dpl, dpr;
for (int i = 0; i < n; i++) {
(i < m ? L : R).push_back({a[i].second, a[i].first});
(i < m ? dpl : dpr).push_back(dp[id[a[i]]]);
}
vector<ld> res(n);
{
auto resl = solve(L, dpl);
vector<ld> dpr2(dpr.size(), 1e18);
for (int i = m; i < n; i++) {
for (int j = 0; j < m; j++) {
dpr2[i - m] = min(dpr2[i - m], resl[j] + kek(a[i], a[j]));
}
}
auto resr = solve(R, dpr2);
for (int i = m; i < n; i++) {
res[i] = resr[i - m];
}
}
{
auto resr = solve(R, dpr);
vector<ld> dpr2(dpl.size(), 1e18);
for (int i = m; i < n; i++) {
for (int j = 0; j < m; j++) {
dpr2[j] = min(dpr2[j], resr[i - m] + kek(a[i], a[j]));
}
}
auto resl = solve(L, dpr2);
for (int i = 0; i < m; i++) {
res[i] = resl[i];
}
}
vector<ld> result = res;
for (int i = 0; i < n; i++) {
result[id[a[i]]] = res[i];
}
return result;
}
vector<int> restore(vector<pair<int, int>> a, vector<ld> dp, int mx) {
const int n = (int) a.size();
if (n == 1) {
return vector<int>{0};
}
map<pair<int, int>, int> id;
for (int i = 0; i < n; i++) {
id[a[i]] = i;
}
auto p = a[mx];
std::sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
if (a[i] == p) {
mx = i;
}
}
const int m = n / 2;
vector<pair<int, int>> L, R;
vector<ld> dpl, dpr;
for (int i = 0; i < n; i++) {
(i < m ? L : R).push_back({a[i].second, a[i].first});
(i < m ? dpl : dpr).push_back(dp[id[a[i]]]);
}
if (mx >= m) {
auto resl = solve(L, dpl);
vector<ld> dpr2(dpr.size(), 1e18);
for (int i = m; i < n; i++) {
for (int j = 0; j < m; j++) {
dpr2[i - m] = min(dpr2[i - m], resl[j] + kek(a[i], a[j]));
}
}
auto resr = restore(R, dpr2, mx - m);
for (auto& x : resr) {
x += m;
}
for (int j = 0; j < m; j++) {
if (equal(resl[j] + kek(a[resr.back()], a[j]), dpr2[resr.back() - m])) {
auto resl = restore(L, dpl, j);
for (auto x : resl) {
resr.push_back(x);
}
for (auto& x : resr) {
x = id[a[x]];
}
return resr;
}
}
// assert(false);
} else {
auto resr = solve(R, dpr);
vector<ld> dpr2(dpl.size(), 1e18);
for (int i = m; i < n; i++) {
for (int j = 0; j < m; j++) {
dpr2[j] = min(dpr2[j], resr[i - m] + kek(a[i], a[j]));
}
}
auto resl = restore(L, dpr2, mx);
for (int j = m; j < n; j++) {
if (equal(resr[j - m] + kek(a[resl.back()], a[j]), dpr2[resl.back()])) {
auto resr = restore(R, dpr, j - m);
for (auto x : resr) {
resl.push_back(x + m);
}
for (auto& x : resl) {
x = id[a[x]];
}
return resl;
}
}
// assert(false);
}
vector<int> res(n);
std::iota(res.begin(), res.end(), 0);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
vector<ld> dp(n, 0);
auto ans = solve(a, dp);
int pos = min_element(ans.begin(), ans.end()) - ans.begin();
cout << fixed << setprecision(15) << ans[pos] << '\n';
auto result = restore(a, dp, pos);
reverse(result.begin(), result.end());
for (auto x : result) {
cout << x + 1 << " ";
}
cout << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4000kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.383325771613344 7 3 4 2 8 5 1 6
result:
ok correct!
Test #2:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
20 4 71 52 7 49 15 59 83 12 9 46 6 74 44 89 50 32 10 82 58 11 33 78 72 27 49 64 75 97 0 38 46 91 54 8 70 18 61 79 92
output:
374.883681117479741 14 4 20 12 10 17 8 15 7 2 6 3 9 5 11 16 13 19 18 1
result:
ok correct!
Test #3:
score: 0
Accepted
time: 2ms
memory: 3880kb
input:
100 192 64 839 68 846 688 911 133 110 439 592 226 355 364 418 487 402 466 436 425 509 847 542 78 648 404 954 313 726 906 777 922 596 550 159 172 507 651 720 932 575 805 889 193 246 206 175 326 897 464 108 70 790 2 548 624 867 222 743 269 41 98 348 173 49 915 35 939 404 571 371 625 363 758 317 155 90...
output:
8491.452160233976989 31 26 86 1 98 18 93 79 5 45 90 24 23 38 42 57 32 56 85 49 67 7 10 9 41 63 83 62 96 47 39 33 34 48 72 91 60 61 88 11 92 80 99 37 89 36 35 8 82 19 75 28 44 17 71 78 55 97 69 50 58 21 15 20 81 16 74 66 76 87 3 46 54 77 95 68 73 25 52 43 14 84 4 22 29 40 2 27 100 53 12 65 6 30 64 51...
result:
ok correct!
Test #4:
score: 0
Accepted
time: 68ms
memory: 3984kb
input:
500 380 190 314 738 973 705 875 517 593 638 132 681 714 411 400 589 139 353 339 771 832 883 610 170 925 431 351 96 884 683 674 817 5 386 710 99 348 173 996 812 533 453 851 10 877 142 86 361 860 168 489 50 641 766 158 118 989 745 823 559 374 971 605 158 432 307 931 863 719 635 73 341 726 906 536 372 ...
output:
22821.888978355533935 268 48 322 189 168 140 264 178 400 430 112 8 162 407 355 111 329 269 472 242 123 195 102 361 311 478 245 413 491 452 261 434 397 460 151 344 485 240 193 489 218 391 464 97 342 494 145 31 373 190 471 403 169 251 385 422 382 395 10 50 155 2 461 60 199 126 236 91 401 276 324 222 3...
result:
ok correct!
Test #5:
score: 0
Accepted
time: 286ms
memory: 4128kb
input:
1000 818 537 491 340 916 881 776 67 368 978 888 853 8 349 561 929 604 8 349 828 874 894 257 757 667 962 242 746 3 614 931 863 235 578 516 580 61 177 13 821 949 165 231 732 970 21 711 731 392 374 878 672 106 596 82 166 149 539 944 485 481 675 845 636 352 185 326 4 229 472 617 972 622 175 83 554 447 7...
output:
32684.782659301325133 763 794 126 659 675 593 108 402 34 998 45 259 781 715 943 896 220 520 961 459 410 590 700 477 383 33 446 845 834 359 428 461 247 690 78 362 418 805 233 701 955 216 806 403 429 57 176 608 460 363 162 582 324 159 124 509 371 823 737 473 239 542 725 237 513 932 971 934 565 113 512...
result:
ok correct!
Test #6:
score: -100
Wrong Answer
time: 392ms
memory: 4132kb
input:
1000 94954 408313 589670 644618 101544 170845 308094 798263 871557 182716 42389 153936 777317 429523 812471 38482 979047 249000 967597 351300 982071 356744 369070 837238 661606 876392 70400 544589 460840 381840 151672 220775 539578 774105 717079 505259 241023 619236 318139 186353 39127 718711 697393...
output:
1000000000000000000.000000000000000 711 31 680 803 371 685 544 67 164 545 967 120 293 200 364 146 792 292 114 198 339 460 707 979 346 286 912 592 307 868 947 554 782 39 49 757 756 986 562 921 490 122 139 321 495 555 929 542 196 288 35 752 515 416 269 679 13 890 796 380 34 290 958 54 534 78 426 577 3...
result:
wrong answer The first line of your output is 1000000000000000000.000000, but the length of jury's solution is 32281914.252424