QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#691504#2685. Driving in OptimistankevinyangAC ✓28ms14356kbC++174.1kb2024-10-31 11:43:112024-10-31 11:43:11

Judging History

This is the latest submission verdict.

  • [2024-10-31 11:43:11]
  • Judged
  • Verdict: AC
  • Time: 28ms
  • Memory: 14356kb
  • [2024-10-31 11:43:11]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double

#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()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


double f(double l, double r, double k){
    // if(l > r){
    //     swap(l,r);
    // }
    return k*l*r + (r-l)*k*(k-1)/2 - (k-1)*k*(2*k-1)/6;
}

int f2(int l, int r, int k){
    int ans = 0;
    if(l > r){
        swap(l,r);
    }
    for(int i = 0; i<k; i++){
        ans+=(l+i)*(r-i);
    }
    return ans;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<vector<int>>d(n+1,vector<int>(n+1));
    for(int i = 1; i<=n; i++){
        for(int j = i+1; j<=n; j++){
            cin >> d[i][j];
            d[j][i] = d[i][j];
        }
    }
    vector<vector<int>>dep(n+1,vector<int>(n+1)); // dep[i][j] is the depth of the lca(i,j)
    for(int i = 2; i<=n; i++){
        for(int j = 2; j<=n; j++){
            if(i==j)continue;
            if((d[1][i] + d[1][j] - d[i][j])%2 !=0){
                cout << "impossible\n";
                return 0;
            }
            dep[i][j] = (d[1][i] + d[1][j] - d[i][j])/2;
        }
    }
    vector<vector<pii>>adj(2*n+5);
    int label = n+1;
    int tot = 0;
    auto solve = [&](auto self, vector<int>nodes, int parent, int w) -> void {
        int mn = 1e9;
        int m = nodes.size();
        if(nodes.size() == 0)return;
        if(nodes.size() == 1){
            int u = nodes[0];
            int depth = d[1][u];
            adj[parent].push_back({depth - w, u});
            adj[u].push_back({depth - w,parent});
            tot += depth-w;
            return;
        }
        int cur = label++;
        
        vector<vector<int>>buckets(m);
        for(int i = 0; i<nodes.size(); i++){
            for(int j = i+1; j<nodes.size(); j++){
                mn = min(mn,dep[nodes[i]][nodes[j]]);
            }
        }
        tot += mn-w;
        adj[parent].push_back({mn-w,cur});
        adj[cur].push_back({mn-w,parent});
        for(int u : nodes){
            //cout << "nodes " << u << '\n';
            for(int i = 0; i<m; i++){
                if(buckets[i].size() == 0){
                    buckets[i].push_back(u);
                    break;
                }
                if(dep[buckets[i][0]][u] != mn){
                    buckets[i].push_back(u);
                    break;
                }
            }
        }
        for(auto vec: buckets){
            self(self, vec, cur, mn);
        }
    };
    vector<int>vec;
    for(int i = 2; i<=n; i++){
        vec.push_back(i);
    }
    solve(solve,vec,1,0);
    // for(int i = 1; i<=2*n; i++){
    //     for(auto [w,y] : adj[i]){
    //         if(i<y)cout << i << ' ' << y << ' ' << w << '\n';
    //     }
    // }
    for(int i = 1; i<=n; i++){
        vector<int>dis(2*n+1);
        vector<bool>vis(2*n+1);
        vis[i] = true;
        queue<int>q;
        q.push(i);
        while(q.size()){
            int cur = q.front(); q.pop();
            for(auto [w,nxt]: adj[cur]){
                if(vis[nxt])continue;
                vis[nxt] = true;
                dis[nxt] = dis[cur] + w;
                q.push(nxt);
            }
        }
        for(int j = 1; j<=n; j++){
            if(d[i][j] != dis[j]){
                cout << "impossible\n";
                return 0;
            }
        }
    }
    double ans = 0;
    vector<int>dp(2*n+1);
    auto dfs = [&](auto self, int u, int p, int w) -> void {
        for(auto [wt, nxt] : adj[u]){
            if(nxt==p)continue;
            self(self,nxt,u,wt);
            dp[u]+=dp[nxt];
            dp[u]+=wt;
        }
        //cout << u << ' ' << p << ' '<< dp[u]+1 << ' ' << tot-dp[u] << ' ' << w << '\n';
        if(w > 0){
            ans += f(dp[u]+1,tot-dp[u],w);
        }
    }; 
    dfs(dfs,1,0,0);
    ans /= (double)tot * (tot+1)/2;
    cout << fixed << setprecision(15);
    cout << ans << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4044kb

input:

4
4 4 4
2 4
4

output:

2.392857142857143

result:

ok 

Test #2:

score: 0
Accepted
time: 6ms
memory: 7800kb

input:

500
30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30000 30...

output:

14980.998002622488539

result:

ok 

Test #3:

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

input:

4
8 8 8
8 8
8

output:

4.117647058823529

result:

ok 

Test #4:

score: 0
Accepted
time: 7ms
memory: 7888kb

input:

500
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 ...

output:

499334.331335998673410

result:

ok 

Test #5:

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

input:

8
3 5 9 9 9 9 6
4 8 8 8 8 5
6 6 6 6 5
4 4 4 9
4 4 9
2 9
9

output:

4.489473684210526

result:

ok 

Test #6:

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

input:

10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

output:

3.666666666666667

result:

ok 

Test #7:

score: 0
Accepted
time: 26ms
memory: 14356kb

input:

500
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...

output:

167.000000000000000

result:

ok 

Test #8:

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

input:

3
1 2
1

output:

1.333333333333333

result:

ok 

Test #9:

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

input:

3
68114 27269
95383

output:

31795.000000000000000

result:

ok 

Test #10:

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

input:

33
76343 25516 95573 90974 33338 43607 85293 84817 143318 70442 125138 11321 46200 43695 102856 102526 132148 63216 78650 158159 64459 107746 31671 40191 103278 75346 64908 130744 45551 74058 105642 107038
101859 171916 167317 109681 119950 161636 161160 219661 146785 201481 87664 122543 120038 1791...

output:

103899.078480326927732

result:

ok 

Test #11:

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

input:

22
210574 287120 145814 134534 107479 28142 140720 148596 132052 203854 110799 105504 53090 243080 200736 88366 106405 161167 123666 47681 193980
146956 183276 171996 144941 210334 174562 162842 169514 237696 144641 142966 168302 102916 60572 122208 143867 195009 161128 162893 227822
259822 248542 2...

output:

98935.584391289172487

result:

ok 

Test #12:

score: 0
Accepted
time: 8ms
memory: 6556kb

input:

398
196706 363893 333780 132063 442534 412206 556140 503709 547975 239849 606381 482418 463089 344803 580596 102921 163867 567195 261501 603688 693298 580533 593052 526143 313065 537304 268779 441019 169146 713324 394164 348083 531400 273252 527028 442167 553106 586280 438994 456273 566268 394334 46...

output:

339450.761885868538059

result:

ok 

Test #13:

score: 0
Accepted
time: 8ms
memory: 7676kb

input:

476
325681 85479 139450 211658 146979 119986 248455 238796 225428 156179 117636 199355 192257 10784 142879 219510 146496 186701 166227 201708 176378 174396 99419 218146 110125 77951 225565 227528 297899 133667 167210 211591 122693 292440 294522 221185 185335 235171 271997 214505 130400 74769 195970 ...

output:

289997.780689377820948

result:

ok 

Test #14:

score: 0
Accepted
time: 8ms
memory: 7900kb

input:

500
293981 456228 333984 247455 348419 343077 349317 68287 123311 267626 371660 248765 377773 266133 266799 86092 189114 321867 124591 471031 255386 206459 240235 275645 244527 454839 257439 342388 334310 310638 376938 222677 175503 322757 328268 309817 453734 525485 239614 239958 226518 402351 2904...

output:

337116.292575667919976

result:

ok 

Test #15:

score: 0
Accepted
time: 6ms
memory: 6088kb

input:

286
531289 100722 717127 358951 14812 389203 576871 700703 135638 330185 118162 165635 131181 157389 683795 314544 69548 96901 267254 603305 160727 584049 270787 296914 341644 397488 322019 242227 361487 41706 55215 290608 199267 216809 318836 527878 260296 130476 596490 374651 322713 519800 308059 ...

output:

259460.860331310029494

result:

ok 

Test #16:

score: 0
Accepted
time: 3ms
memory: 6312kb

input:

289
515581 46138 394760 724435 223271 255242 713952 543024 53846 526203 763208 96876 222080 405099 79563 630025 77017 659579 224604 395491 673473 566269 717655 513644 875701 470188 664044 577558 299235 259235 733855 264970 145163 91094 71820 531040 446751 26758 277091 632952 774954 516303 602251 625...

output:

310230.472119559623593

result:

ok 

Test #17:

score: 0
Accepted
time: 14ms
memory: 10756kb

input:

500
220225 223662 230035 57417 519951 323106 596694 27482 121272 168028 35925 155875 250373 84645 23602 96079 465208 194346 92154 262925 96913 214415 316916 64397 236457 458945 401864 117910 429281 461297 102342 104456 38608 387759 106271 468012 12063 490021 97575 46978 25421 15102 86913 17948 50187...

output:

326388.333333333333343

result:

ok 

Test #18:

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

input:

2
1000000

output:

333334.000000000000000

result:

ok 

Test #19:

score: 0
Accepted
time: 20ms
memory: 11064kb

input:

500
11242 13578 9764 17746 20190 26634 27454 31563 35645 37954 44872 48178 52510 58838 57482 63072 66751 66794 65329 69887 71067 77329 81551 87903 88266 92286 90952 95787 96700 98152 99867 105452 107644 107286 109267 111311 112398 114235 118828 126285 126202 128904 129994 131720 139008 142403 145596...

output:

269701.163380988573181

result:

ok 

Test #20:

score: 0
Accepted
time: 19ms
memory: 11228kb

input:

500
8000 12000 16000 20000 24000 28000 32000 36000 40000 44000 48000 52000 56000 60000 64000 68000 72000 76000 80000 84000 88000 92000 96000 100000 104000 108000 112000 116000 120000 124000 128000 132000 136000 140000 144000 148000 152000 156000 160000 164000 168000 172000 176000 180000 184000 18800...

output:

334660.827006631824474

result:

ok 

Test #21:

score: 0
Accepted
time: 24ms
memory: 14228kb

input:

500
5276 4576 7018 8391 12728 12427 16223 16228 17334 21618 26634 28986 28991 30592 32261 33826 38099 39218 41205 42718 46072 46640 47339 48537 53341 53825 55604 56162 60968 60409 59700 63754 64701 65470 66632 67662 72136 72094 76794 79323 83346 83134 86509 88514 88296 90942 94090 95756 95172 98542 ...

output:

330603.583186986824273

result:

ok 

Test #22:

score: 0
Accepted
time: 28ms
memory: 14204kb

input:

500
4008 6012 8016 10020 12024 14028 16032 18036 20040 22044 24048 26052 28056 30060 32064 34068 36072 38076 40080 42084 44088 46092 48096 50100 52104 54108 56112 58116 60120 62124 64128 66132 68136 70140 72144 74148 76152 78156 80160 82164 84168 86172 88176 90180 92184 94188 96192 98196 100200 1022...

output:

333999.158815547441520

result:

ok 

Test #23:

score: 0
Accepted
time: 11ms
memory: 7880kb

input:

486
56532 56532 56532 56532 56532 56532 56532 56532 56532 47110 56532 47110 56532 56532 56532 56532 56532 56532 47110 47110 56532 47110 56532 47110 56532 47110 47110 37688 56532 56532 18844 56532 56532 47110 56532 47110 56532 56532 47110 56532 56532 56532 56532 56532 18844 56532 47110 28266 37688 56...

output:

41258.201204924697638

result:

ok 

Test #24:

score: 0
Accepted
time: 7ms
memory: 7900kb

input:

500
28266 18844 28266 28266 28266 28266 28266 28266 28266 28266 18844 28266 18844 28266 28266 28266 18844 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 18844 28266 28266 28266 28266 28266 18844 28266 28266 28266 28266 18844 28266 28266 28266 28266 28266 28266 28266 28266 28266 28...

output:

20694.618419225575412

result:

ok 

Test #25:

score: 0
Accepted
time: 7ms
memory: 6292kb

input:

384
75376 65954 65954 65954 65954 75376 75376 75376 65954 56532 56532 75376 75376 75376 65954 75376 75376 75376 75376 75376 65954 56532 65954 75376 65954 75376 37688 65954 65954 65954 75376 75376 37688 75376 75376 75376 75376 56532 75376 75376 18844 75376 75376 75376 75376 75376 75376 65954 75376 18...

output:

54257.903643666634963

result:

ok 

Test #26:

score: 0
Accepted
time: 7ms
memory: 7908kb

input:

495
65954 65954 56532 47110 42399 37688 65954 61243 4711 47110 37688 65954 37688 18844 61243 37688 32977 61243 14133 56532 47110 56532 61243 51821 56532 37688 65954 56532 61243 65954 37688 37688 51821 9422 61243 32977 65954 56532 65954 56532 65954 56532 51821 61243 28266 56532 65954 65954 56532 6595...

output:

44770.551164801231646

result:

ok 

Test #27:

score: 0
Accepted
time: 8ms
memory: 7292kb

input:

461
28266 28266 18844 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 23555 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 28266 23555 28266 18844 23555 18844 23555 23555 28266 28266 28266 23555 28266 28266 23555 28266 18844 28266 28266 28266 28266 28...

output:

21033.364081382673303

result:

ok 

Test #28:

score: 0
Accepted
time: 8ms
memory: 7156kb

input:

449
28266 28266 9422 23555 28266 28266 28266 28266 28266 28266 28266 23555 28266 23555 28266 28266 28266 28266 28266 28266 28266 23555 23555 28266 28266 23555 28266 28266 14133 14133 28266 28266 28266 28266 28266 23555 18844 28266 28266 18844 23555 28266 28266 28266 9422 18844 28266 28266 28266 2826...

output:

20480.389059419463083

result:

ok 

Test #29:

score: 0
Accepted
time: 11ms
memory: 8036kb

input:

499
42399 84798 84798 84798 84798 84798 84798 80087 84798 56532 84798 84798 84798 84798 84798 84798 84798 18844 84798 75376 80087 84798 80087 18844 84798 42399 75376 84798 84798 84798 84798 80087 84798 42399 84798 84798 84798 75376 80087 84798 84798 32977 84798 80087 84798 84798 84798 84798 18844 94...

output:

71895.216593378444131

result:

ok