Submission #498264

# Submission time Handle Problem Language Result Execution time Memory
498264 2021-12-24T17:41:44 Z beepbeepsheep Akvizna (COCI19_akvizna) C++17
130 / 130
647 ms 1616 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define endl '\n'
typedef pair<ll,ll> ii;
typedef pair<ld,ll> ldll;
const ll mod=1e9+7;
const ll maxn=3e5+5;
const ll inf=1e15;
ll n,k;
struct line{
    ld m,c;
    ll used;
    line():m(0),c(0),used(0){}
    line(ld M, ld C, ll Used):m(M),c(C),used(Used){}
    ld operator()(ld x){return m*x+c;}
    ld POI(line l){
        return (this->c-l.c)/(l.m-this->m);
    }
    bool toPop(line a, line b){
        return (this->c-a.c)*(b.m-this->m)<=(this->c-b.c)*(a.m-this->m);
    }
};
struct lch{
    deque<line> dq;
    void upd(line a){
        while (dq.size()>1){
            ll s=dq.size();
            if (a.toPop(dq[s-1],dq[s-2])) dq.pop_back();
            else break;
        }
        dq.push_back(a);
    }
    ldll query(ld p){
        while (dq.size()>1){
            if (dq[0](p)<dq[1](p)) dq.pop_front();
            else if (abs(dq[0](p)-dq[1](p))<=1e-16 && dq[1].used<dq[0].used) dq.pop_front();
            else break;
        }
        return make_pair(dq[0](p),dq[0].used);
    }
}*hull; //max hull
ldll solve(ld cost){
    hull->dq.clear();
    line l;
    l.m=(ld)1/n,l.c=0,l.used=0;
    hull->upd(l);
    ld temp=0;
    for (int i=1;i<=n;i++){
        ldll p = hull->query(i);
        temp = p.first - cost;
        if (i==n){
            return make_pair(temp,p.second+1);
        }
        l.m=(ld)1/(n-i),l.c=temp-((ld) i/(n-i)),l.used=p.second+1;
        hull->upd(l);
    }
    return {0,0};
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    ld l=0,r=1,m;
    ld ans=0;
    hull=new lch();
    ll cnt=0;
    while (cnt!=100){
        m=(l+r)/2;
        ldll p=solve(m);
        if (p.second>k){
            l=m;//need higher cost
        } else{
            ans=m*p.second+p.first;
            r=m;
        }
        cnt++;
    }
    cout<<setprecision(10)<<ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 344 KB Output is correct
2 Correct 18 ms 348 KB Output is correct
3 Correct 14 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 332 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 14 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 340 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 13 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 13 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 344 KB Output is correct
2 Correct 18 ms 332 KB Output is correct
3 Correct 15 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 332 KB Output is correct
2 Correct 18 ms 348 KB Output is correct
3 Correct 15 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 14 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 344 KB Output is correct
2 Correct 19 ms 340 KB Output is correct
3 Correct 15 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 332 KB Output is correct
2 Correct 17 ms 344 KB Output is correct
3 Correct 14 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 582 ms 1356 KB Output is correct
2 Correct 576 ms 1488 KB Output is correct
3 Correct 452 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 600 ms 1484 KB Output is correct
2 Correct 577 ms 1468 KB Output is correct
3 Correct 495 ms 1468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 599 ms 1460 KB Output is correct
2 Correct 578 ms 1616 KB Output is correct
3 Correct 532 ms 1472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 1484 KB Output is correct
2 Correct 585 ms 1488 KB Output is correct
3 Correct 479 ms 1352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 609 ms 1400 KB Output is correct
2 Correct 576 ms 1608 KB Output is correct
3 Correct 484 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 647 ms 1484 KB Output is correct
2 Correct 569 ms 1488 KB Output is correct
3 Correct 499 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 645 ms 1500 KB Output is correct
2 Correct 515 ms 1460 KB Output is correct
3 Correct 472 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 581 ms 1356 KB Output is correct
2 Correct 580 ms 1488 KB Output is correct
3 Correct 480 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 635 ms 1484 KB Output is correct
2 Correct 583 ms 1468 KB Output is correct
3 Correct 505 ms 1504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 621 ms 1484 KB Output is correct
2 Correct 561 ms 1488 KB Output is correct
3 Correct 503 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 634 ms 1484 KB Output is correct
2 Correct 585 ms 1488 KB Output is correct
3 Correct 506 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 629 ms 1476 KB Output is correct
2 Correct 583 ms 1468 KB Output is correct
3 Correct 536 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 629 ms 1468 KB Output is correct
2 Correct 577 ms 1488 KB Output is correct
3 Correct 524 ms 1460 KB Output is correct