QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#387037#5111. Take On MemeEnergy_is_not_overTL 2ms10128kbC++176.0kb2024-04-11 23:51:292024-04-11 23:51:30

Judging History

你现在查看的是最新测评结果

  • [2024-04-11 23:51:30]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:10128kb
  • [2024-04-11 23:51:29]
  • 提交

answer

//
// Stvoreno ENERGom o 11.04.24. 16:40:54
//

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define F first
#define fir first
#define S second
#define sec second
#define mp make_pair
#define MP make_pair
#define pb push_back
#define PB push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

#ifdef Energy
#define DEBUG for (int _____DEBUG=1;_____DEBUG;_____DEBUG=0)

template<class ...Ts>
auto &PRNT(Ts ...ts) { return ((cerr << ts << " "), ...); }

#define LOG(...) PRNT(#__VA_ARGS__" ::",__VA_ARGS__)<<endl
#else
#define DEBUG while (0)
#define LOG(...)
#endif

const int max_n = 10011, inf = 1000111222;
const ld pi = 2 * acosl(0.0l);
const int max_its = 50;

struct Point {
    int x, y;

    Point operator - (const Point &p) const {
        return {x - p.x, y - p.y};
    }
};

int n;
vector<int> g[max_n];
Point p[max_n];
vector<Point> points;

vector<ld> angs;

void add(ld ang) {
    if (ang >= 2 * pi) {
        ang -= 2 * pi;
    }
    angs.push_back(ang);
}

const ld phi = (sqrtl(5) - 1) / 2;
const ld phi1 = 1 - phi;

const int max_cands = 4 * max_n;

ld val[max_n];
pair<ld, pair<int, int>> dp[max_n][2], tmp[2][2];
void upd(pair<ld, pair<int, int>> &a, const pair<ld, pair<int, int>> &b) {
    a = max(a, b);
}

pair<ld, pair<int, int>> comb(const pair<ld, pair<int, int>> &a, const pair<ld, pair<int, int>> &b) {
    return {a.first + b.first, {a.second.first + b.second.first, a.second.second + b.second.second}};
}
int q1 = 0, q2 = 1;

void calc(int v) {
    for (int to : g[v]) {
        calc(to);
    }
    for (int f = 0; f < 2; ++f) {
        int sign = 1 - 2 * f;
        if (g[v].empty()) {
            dp[v][f] = {sign * val[v], {p[v].x * sign, p[v].y * sign}};
            continue;
        }
        tmp[q1][0] = {-1e18, {0, 0}};
        tmp[q1][1] = {-1e18, {0, 0}};
        tmp[q1][0] = {0, {0, 0}};
        for (int to : g[v]) {
            tmp[q2][0] = {-1e18, {0, 0}};
            tmp[q2][1] = {-1e18, {0, 0}};
            for (int cur = 0; cur < 2; ++cur) {
                for (int take = 0; cur + take < 2; ++take) {
                    upd(tmp[q2][cur + take], comb(tmp[q1][cur], dp[to][f ^ take]));
                }
            }
            swap(q1, q2);
        }
        dp[v][f] = tmp[q1][1];
    }
//    LOG(v, dp[v][0].first, dp[v][1].first);
}

pair<ld, ll> f(ld ang) {
    ld dx = cosl(ang), dy = sinl(ang);
    const ld A = -dy, B = dx, C = 0;
    const ld SQ = sqrtl(A * A + B * B);
    for (int i = 0; i < n; ++i) {
        if (g[i].empty()) {
            ld dd = (A * p[i].x + B * p[i].y + C) / SQ;
            ld dot = (dx * p[i].x + dy * p[i].y);
            val[i] = sqrtl(p[i].x * p[i].x + p[i].y * p[i].y - dd * dd);
            if (dot < 0) {
                val[i] *= -1;
            }
//            LOG(i, dd, val[i]);
        }
    }
//    LOG(val[3] + val[2] + val[1]);
    calc(0);
    return {dp[0][0].first, 1LL * dp[0][0].second.first * dp[0][0].second.first + 1LL * dp[0][0].second.second * dp[0][0].second.second};
}

struct Data {
    ld L, R, m1, m2;
    pair<ld, ll> ans1, ans2;

    pair<ld, ll> get_ans() {
        return max(ans1, ans2);
    }

    void init(ld LL, ld RR) {
        L = LL;
        R = RR;
        m1 = L * phi + R * phi1;
        ans1 = f(m1);
        m2 = L * phi1 + R * phi;
        ans2 = f(m2);
    }

    void iter() {
        if (ans1 > ans2) {
            R = m2;
            m2 = m1;
            ans2 = ans1;
            m1 = L * phi + R * phi1;
            ans1 = f(m1);
        } else {
            L = m1;
            m1 = m2;
            ans1 = ans2;
            m2 = L * phi1 + R * phi;
            ans2 = f(m2);
        }
    }

    ll get_ok_ans() {
        return max(ans1.second, ans2.second);
    }
};

Data d[max_cands];

void dfs(int v, int h = 0) {
    for (int to : g[v]) {
        dfs(to, h ^ 1);
    }
    if (g[v].empty()) {
        if (h) {
            p[v].x *= -1;
            p[v].y *= -1;
        }
    }
}

int main() {
//    freopen("input_k.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        if (!k) {
            cin >> p[i].x >> p[i].y;
            LOG(p[i].x, p[i].y);
        } else {
            while (k--) {
                int to;
                cin >> to;
                --to;
                g[i].push_back(to);
            }
        }
    }
    dfs(0);
    for (int i = 0; i < n; ++i) {
        if (p[i].x || p[i].y) {
            points.push_back(p[i]);
        }
    }
    if (0) {
        auto [d1, d2] = f(atan2(-12, 5));
        LOG(d1, d2);
        LOG(d1 * d1);
        return 0;
    }
    angs.push_back(0);
    angs.push_back(2 * pi);
    for (auto p : points) {
        ld ang = atan2l(p.y, p.x);
        if (ang < 0) {
            ang += 2 * pi;
        }
        add(ang + pi / 2);
        add(ang + 3 * pi / 2);
    }
    sort(angs.begin(), angs.end());
    vector<pair<pair<ld, ll>, int>> cands, ncands;
    for (int i = 0; i + 1 < angs.size(); ++i) {
        d[i].init(angs[i], angs[i + 1]);
        cands.push_back({{0, 0}, i});
    }
    ll ans = 0;
    for (int it = 0; it < max_its; ++it) {
        ncands.clear();
        for (auto [P, id] : cands) {
            d[id].iter();
            ans = max(ans, d[id].get_ok_ans());
            ncands.push_back({d[id].get_ans(), id});
        }
        int to_leave = ncands.size();
        to_leave = 1;
        if (it > 3) {
//            to_leave *= 0.74;
//            to_leave = max(to_leave, 47);
        }
        nth_element(ncands.begin(), ncands.begin() + to_leave, ncands.end(), greater<pair<pair<ld, ll>, int>>());
        if (ncands.size() > to_leave) {
            ncands.resize(to_leave);
        }
        cands.swap(ncands);
    }
    cout << ans << "\n";
    exit(0);
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9212kb

input:

5
4 2 3 4 5
0 2 -2
0 1 3
0 4 -6
0 -18 5

output:

725

result:

ok single line: '725'

Test #2:

score: 0
Accepted
time: 2ms
memory: 10128kb

input:

5
2 2 3
2 4 5
0 1 5
0 -4 -6
0 -1 7

output:

340

result:

ok single line: '340'

Test #3:

score: 0
Accepted
time: 0ms
memory: 9920kb

input:

18
3 4 3 2
2 5 6
3 7 9 8
3 10 11 12
0 4 -1
0 18 49
0 -2 10
2 13 14
0 -5 6
0 5 8
4 15 16 17 18
0 17 3
0 3 -9
0 -7 -1
0 14 -33
0 -23 11
0 11 14
0 2 19

output:

26269

result:

ok single line: '26269'

Test #4:

score: -100
Time Limit Exceeded

input:

10000
59 2 171 340 509 678 847 1016 1185 1382 1551 1720 1889 2058 2227 2396 2565 2734 2903 3072 3241 3410 3579 3748 3917 4086 4255 4424 4593 4762 4931 5100 5269 5438 5607 5776 5945 6114 6283 6452 6621 6790 6959 7128 7297 7466 7635 7804 7973 8142 8311 8480 8649 8818 8987 9156 9325 9494 9663 9832
2 3 ...

output:


result: