QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#726510 | #3858. Systematic salesman | ahsoltan | WA | 2ms | 10076kb | C++20 | 3.6kb | 2024-11-09 01:54:45 | 2024-11-09 01:54:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif
template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct Point {
typedef Point P;
T x, y;
auto operator<=>(const P&) const = default;
P operator+(P p) const { return P(x+p.x, y+p.y); }
P operator-(P p) const { return P(x-p.x, y-p.y); }
P operator*(T d) const { return P(x*d, y*d); }
P operator/(T d) const { return P(x/d, y/d); }
T dot(P p) const { return x*p.x + y*p.y; }
T cross(P p) const { return x*p.y - y*p.x; }
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
T dist2() const { return x*x + y*y; }
double dist() const { return sqrt((double)dist2()); }
// angle to x-axis in interval [-pi, pi]
double angle() const { return atan2(y, x); }
P unit() const { return *this/dist(); } // makes dist()=1
P perp() const { return P(-y, x); } // rotates +90 degrees
P normal() const { return perp().unit(); }
// returns point rotated 'a' radians ccw around the origin
P rotate(double a) const {
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
friend ostream& operator<<(ostream& os, P p) {
return os << "(" << p.x << "," << p.y << ")"; }
};
using P = Point<ll>;
bool chmin(double& x, double y) { return y < x ? x = y, 1 : 0; }
const int N = 1010;
int n;
P p[N];
double dp[N][N], f[N][N];
int s[N][N], sf[N][N];
vector<pii> rec(vi v, int d) {
int m = sz(v);
if (m == 1) {
int i = v[0];
dp[i][i] = 0;
s[i][i] = sf[i][i] = i;
return {{i, i}};
}
nth_element(v.begin(), (m + 1) / 2 + all(v), [&](int x, int y) {
if (d % 2 == 0) return p[x].x < p[y].x;
else return p[x].y < p[y].y;
});
vi l(v.begin(), v.begin() + m / 2), r(m / 2 + all(v));
auto lw = rec(l, d + 1); auto pr = rec(r, d + 1);
for (auto [i, j] : lw) for (int k : r) {
if (chmin(f[i][k], dp[i][j] + (p[k] - p[j]).dist())) {
sf[i][k] = j;
}
}
for (int i : l) for (auto [j, k] : pr) {
if (chmin(dp[i][k], f[i][j] + dp[j][k])) {
s[i][k] = j;
}
}
for (auto [i, j] : pr) for (int k : l) {
if (chmin(f[i][k], dp[i][j] + (p[k] - p[j]).dist())) {
sf[i][k] = j;
}
}
for (int i : r) for (auto [j, k] : lw) {
if (chmin(dp[i][k], f[i][j] + dp[j][k])) {
s[i][k] = j;
}
}
vector<pii> x;
x.reserve(2 * sz(l) * sz(r));
for (int i : l) for (int j : r) {
x.push_back({i, j});
x.push_back({j, i});
}
return x;
}
vi ans;
void buduj(int a, int b) {
if (a == b) {
ans.push_back(a);
return;
}
int y = s[a][b];
int x = sf[a][y];
buduj(a, x);
buduj(y, b);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
rep(i, 0, n) cin >> p[i].x >> p[i].y;
vi v(n); iota(all(v), 0);
rep(i, 0, n) rep(j, 0, n) f[i][j] = dp[i][j] = 1 / .0;
auto pr = rec(v, 0);
pair<double, pii> bst = {1 / .0, {}};
for (auto [i, j] : pr) bst = min(bst, {dp[i][j], {i, j}});
buduj(bst.second.first, bst.second.second);
cout << fixed << setprecision(12) << bst.first << '\n';
rep(i, 0, n) cout << ans[i] + 1 << " \n"[i == n - 1];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10076kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.383325771613 6 1 5 8 2 4 3 7
result:
ok correct!
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 10068kb
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:
468.909811900288 16 13 18 1 19 11 9 5 3 6 2 8 15 7 17 10 20 14 4 12
result:
wrong answer The first line of your output is 468.909812, but the length of jury's solution is 374.883681