QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#378090#2173. What's Our Vector, Victor?distant_yesterdayRE 113ms10052kbC++206.0kb2024-04-06 01:07:342024-04-06 01:07:35

Judging History

This is the latest submission verdict.

  • [2024-04-06 01:07:35]
  • Judged
  • Verdict: RE
  • Time: 113ms
  • Memory: 10052kb
  • [2024-04-06 01:07:34]
  • Submitted

answer

#include <bits/stdc++.h>
#ifdef LOCAL
#include "cp_debug.h"
#else
#define debug(...) 42
#endif
#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 namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
template<typename T> void write(T x) {
    if(x > 9) write(x/10);
    putchar(x%10 + '0');
}

const double eps = 1e-11;
int sgn(double x) {
    if(fabs(x) < eps) return 0;
    return x > 0 ? 1 : -1;
}

struct Vec : public vector<double> {
    Vec(size_t siz, double val): vector(siz, val) { }
#define v (*this)
    double operator * (const Vec& rhs) const {
        assert(sz(v) == sz(rhs));
        double res = 0;
        for(int i = 0; i < sz(v); ++i)
            res += v[i] * rhs[i];
        return res;
    }
    Vec operator + (const Vec& rhs) const {
        assert(sz(v) == sz(rhs));
        Vec res(sz(v), 0);
        for(int i = 0; i < sz(v); ++i) res[i] = v[i] + rhs[i];
        return res;
    }
    Vec operator - (const Vec& rhs) const {
        assert(sz(v) == sz(rhs));
        Vec res(sz(v), 0);
        for(int i = 0; i < sz(v); ++i) res[i] = v[i] - rhs[i];
        return res;
    }
    Vec operator += (const Vec& rhs) { 
        assert(sz(v) == sz(rhs));
        for(int i = 0; i < sz(v); ++i) v[i] += rhs[i]; return v;
    }
    Vec operator -= (const Vec& rhs) { 
        assert(sz(v) == sz(rhs));
        for(int i = 0; i < sz(v); ++i) v[i] -= rhs[i]; return v; 
    }
    Vec operator * (double k) const { 
        Vec res(v); for(auto &g : res) g *= k; 
        return res;
    }
    Vec operator /= (double k) {
        for(auto &g: v) g /= k; 
        return v;
    }
    void do_normalize() { v /= sqrt(v * v); }
    void show() const {
        for(int i = 0; i < sz(v); ++i)
            printf("%.12f%c", v[i], " \n"[i == sz(v) - 1]);
    }
#undef v
};
double dis(const Vec& A, const Vec& B) { return (A - B) * (A - B); }

// input: m x (d+1) matrix, meaning [A, b] in Ax=b.
// \sum_{j=0}^{d-1} a[i][j] * x[j] = a[i][d].
// returns:
tuple<vi, vi, Vec, vector<Vec>> gaussian_elimination(vector<Vec> a) {
    int m = sz(a), d = sz(a[0])-1;
    int pos = 0, rc = 0;
    vector<int> fre(d), leftp;
    rep(i, 0, d) {
        int op = -1;
        for(int j = pos; j < m; j++) {
            if(sgn(a[j][i])) {
                if(op == -1 || fabs(a[j][i]) > fabs(a[op][i])) {
                    op = j;
                }
            }
        }
        if(op == -1) {
            fre[i] = ++rc;
            continue;
        }
        fre[i] = 0;
        if(op != pos) {
            swap(a[pos], a[op]);
        }
        for(int j = pos+1; j < m; j++) {
            double f = a[j][i] / a[pos][i];
            a[j] -= a[pos] * f;
        }
        a[pos] /= a[pos][i];
        leftp.push_back(i);
        pos++;
    }
    debug(fre, leftp);
    debug("G", a);
    while(sz(a) > 0) {
        const auto &last = a.back();
        bool last_zero = fabs(last[d]) < 1000*eps;
        bool pre_zero = true;
        rep(i,0,d) pre_zero = pre_zero && fabs(last[i]) < 1000*eps;
        if(pre_zero) {
            assert(last_zero);
            a.pop_back();
        } else {
            break;
        }
    }
    assert(sz(a) == sz(leftp));
    assert(sz(a) <= d);
    debug("DROP", a);

    // solution
    Vec sol(d, 0.0);
    for(int i = sz(a)-1; i >= 0; --i) {
        assert(sgn(a[i][leftp[i]] - 1.0) == 0);
        sol[leftp[i]] = a[i][d];
        rep(j,0,i) {
            double f = a[j][leftp[i]];
            a[j] -= a[i] * f;
        }
    }
    debug(sol);

    // orthonormal null space of A.
    vector<Vec> res;
    for(int f = 1; f <= rc; ++f) {
        Vec c(d, 0);
        for(int i = 0; i < d; ++i) if(fre[i] == f) c[i] = 1;
        for(int i = sz(a)-1; i >= 0; --i) {
            for(int j = leftp[i] + 1; j < d; ++j) {
                c[leftp[i]] -= c[j] * a[i][j];
            }
        }
        for(auto g : res) c -= g * (c * g);
        c.do_normalize();
        res.push_back(c);
    }
    debug(res);
    return {fre, leftp, sol, res};
}

void solve() {
    int d, n;
    read(d, n);
    vector<Vec> a(n, Vec(d, 0.0));
    Vec e(n, 0), norm2(n, 0);
    rep(i,0,n) {
        rep(j,0,d) {
            scanf("%lf", &a[i][j]);
            norm2[i] += a[i][j] * a[i][j];
        }
        scanf("%lf", &e[i]);
    }
    // mat[i][d] -- rhs
    vector<Vec> mat(n-1, Vec(d+1, 0.0));
    rep(i,0,n-1) {
        rep(j,0,d) {
            mat[i][j] = (a[i][j]-a[n-1][j]);
        }
        mat[i][d] = 0.5 * ((norm2[i]-norm2[n-1]) - e[i]*e[i] + e[n-1]*e[n-1]);
    }
    debug(mat);
    if(n == 1) {
        rep(i,0,d) {
            printf("%.11f%c", a[0][i]+(i==0?e[0]:0)," \n"[i==d-1]);
        }
        return;
    }
    // the plane -- sol + C*res for any C in R^{dim(null(A))}
    auto [fre, leftp, sol, res] = gaussian_elimination(mat);
    // the ball -- centered at a[n-1] with e[n-1]
    auto min_vec = sol - a[n-1];
    for(auto g: res) {
        min_vec -= g * (min_vec * g);
    }
    double min_vec_length_sqr = min_vec*min_vec;
    assert(min_vec_length_sqr <= e[n-1]*e[n-1]);
    double walk_dist = sqrt(e[n-1]*e[n-1]-min_vec_length_sqr);
    debug(walk_dist);
    auto final_point = a[n-1]+min_vec;
    debug(final_point);
    if(walk_dist >= 1e-6) {
        assert(sz(res) > 0);
        final_point += res[0]*walk_dist;
    }
    rep(i,0,d) {
        printf("%.11f%c", final_point[i]," \n"[i==d-1]);
    }
}

signed main() {
    int T; T = 1;
    while(T--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 113ms
memory: 9820kb

input:

500 500
60.00268893933372283 70.35885554610950976 -8.98574176457012186 46.16014112676185732 66.31422279348288384 29.03050764912902082 -35.06996828144599476 -59.10319321730690234 67.85808505028276727 20.30232033048615392 62.38784996896146140 59.92659390534240060 -36.26787439344059294 30.8251981981496...

output:

19.21314202328 6.61160103841 21.42004827099 56.93864780684 -89.71604746868 92.93228900637 70.95769160007 11.49554277970 -48.36859404984 38.69381072100 50.07742920704 7.62861162425 2.21480565119 -37.31399084287 -95.50612141063 -67.78347667256 -6.43422579941 45.24694340816 -27.51574560991 19.963864243...

result:

ok accepted

Test #2:

score: 0
Accepted
time: 113ms
memory: 9760kb

input:

500 500
-20.46791708061053328 -78.90373004342441732 -47.09421721341704625 49.96697218958462372 48.99528416466444014 -46.87780686216100889 73.68284736943891744 -53.69249802982882613 -92.37158007019175443 -97.95346943385396798 -47.79109018973426259 82.31965483504592385 -72.09718577745493917 -32.376534...

output:

-1.52153450791 4.56939357264 -55.15374000490 -30.82061230786 -4.53007111745 -40.74804909282 -98.08174663875 -70.67943356687 33.75800057876 40.83078719874 -79.97010236613 28.76110614563 74.11693817693 99.20066097363 63.14666159688 13.48723862520 87.81528089994 -79.24851687056 -94.83717688756 -18.8682...

result:

ok accepted

Test #3:

score: 0
Accepted
time: 103ms
memory: 9756kb

input:

500 500
91.75754360584323877 -74.07465057030329092 -30.06919963013949371 -22.45376960826932589 -94.33818252302211249 95.43185666705545600 -35.93139380204742395 -46.28091101025437837 20.25588702053946122 -31.98355965847254367 96.27926708531694544 17.21195108047088240 -16.00480323252260462 -14.1553994...

output:

26.96797408424 -85.51968875917 33.00515250810 56.44917679005 96.20241920519 2.14496497969 46.15930107783 -10.55247728842 95.18780892289 89.03670346651 53.30991399636 47.81093648086 -44.11493615189 -38.68117788513 20.30004648906 -79.55956738281 -83.21872812691 81.04064196283 63.60969293991 -32.594959...

result:

ok accepted

Test #4:

score: 0
Accepted
time: 108ms
memory: 9844kb

input:

500 500
53.59239375857157484 1.29576452891167548 -55.60357761919549802 2.89491049833583247 -72.25117082444128869 -44.92736039007480997 6.81675337705620166 -46.20517542139523925 20.56179110027964896 99.93596620202598046 -30.87710828002636276 60.42621397022924157 79.91458345844398536 94.56182305252031...

output:

25.33192619116 6.05132002660 41.15146068664 -77.21568857925 76.99183184577 80.56904900520 93.06870053691 -33.38783398486 -33.19327080732 -89.27631785303 60.99356088544 21.82363145810 -91.88044701367 76.37558475542 26.48459291937 -41.67736557505 16.22086461857 81.94662678581 29.06951444015 -11.928903...

result:

ok accepted

Test #5:

score: 0
Accepted
time: 109ms
memory: 10052kb

input:

500 500
-63.61543927507285190 43.87221058298641196 8.16924114047013461 -34.92232278546450175 91.80142804719758942 -76.82013016900501157 25.06001121373941487 48.24958448628115093 65.82846176112221315 33.19899446519090702 84.05389321600614494 -78.49637158041961982 87.99406312213668002 -60.859516869891...

output:

8.13485701729 -23.75727998673 -99.86678436737 78.59682054002 4.68676492973 -52.35495792473 -60.65258081640 45.10868303404 75.54516051786 -86.72220987406 46.02924842327 -47.95422161432 -96.31557085191 1.05814364859 63.37179287013 96.96924624431 99.73104802133 87.26535881989 4.43819857235 4.1533345617...

result:

ok accepted

Test #6:

score: -100
Runtime Error

input:

24 114
19.60728784212125220 62.05571106800468328 -77.66722345143722350 83.32150488873739391 85.61849285200199233 -61.26186974500755866 54.01645372221616981 -19.69713224000579999 -10.16958551834513003 -18.69109461054532062 9.59051520125903778 91.21514401069063638 89.60632230190051928 -51.201061482129...

output:


result: