Submission #309943

# Submission time Handle Problem Language Result Execution time Memory
309943 2020-10-05T03:56:35 Z phathnv Akvizna (COCI19_akvizna) C++11
130 / 130
463 ms 3572 KB
#include <bits/stdc++.h>
using namespace std;

#define X first
#define Y second
#define mp make_pair

typedef long long ll;
typedef pair<int, int> ii;

struct line{
    double a, b;
    int cnt;
    line(double _a = 0, double _b = 0, int _cnt = 0){
        a = _a;
        b = _b;
        cnt = _cnt;
    }
};

struct convexHull{
    vector <line> d;
    int ptr;
    void reset(){
        d.clear();
        ptr = 0;
    }
    double calcY(const line &l, ll x){
        return l.a * x + l.b;
    }
    bool bad(const line &l1, const line &l2, const line &l3){
        return (l2.a - l3.a) * (l2.b - l1.b) >= (l1.a - l2.a) * (l3.b - l2.b);
    }
    void add(const line &newLine){
        while (d.size() > 1){
            if (bad(d[d.size() - 2], d[d.size() - 1], newLine))
                d.pop_back();
            else
                break;
        }
        d.push_back(newLine);
    }
    double getMax(double x){
        assert(!d.empty());
        ptr = min(ptr, (int) d.size() - 1);
        while (ptr < (int) d.size() - 1)
            if (calcY(d[ptr], x) <= calcY(d[ptr + 1], x))
                ptr++;
            else
                break;
        return calcY(d[ptr], x);
    }
    int getCnt(double x){
        assert(!d.empty());
        ptr = min(ptr, (int) d.size() - 1);
        while (ptr < (int) d.size() - 1)
            if (calcY(d[ptr], x) <= calcY(d[ptr + 1], x))
                ptr++;
            else
                break;
        return d[ptr].cnt;
    }
} cvh;

int n, k;

void readInput(){
    cin >> n >> k;
}

int calcCnt(double cost){
    cvh.reset();
    double val = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        cvh.add(line(val, -(i - 1), cnt));
        val = cvh.getMax(i) / i + 1 - cost;
        cnt = cvh.getCnt(i) + 1;
    }
    return cnt;
}

double calcVal(double cost){
    cvh.reset();
    double val = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        cvh.add(line(val, -(i - 1), cnt));
        val = cvh.getMax(i) / i + 1 - cost;
        cnt = cvh.getCnt(i) + 1;
    }
    return val;
}

void solve(){
    double l = 0, r = 1, best = -1;
    for(int t = 1; t <= 200; t++){
        double mid = (l + r) / 2.0;
        if (calcCnt(mid) <= k){
            best = mid;
            r = mid;
        } else {
            l = mid;
        }
    }
    calcCnt(best);
    cout << fixed << setprecision(20) << calcVal(best) + best * k;
}

int main(){
    readInput();
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 12 ms 524 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 12 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 14 ms 512 KB Output is correct
3 Correct 14 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 12 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 512 KB Output is correct
2 Correct 13 ms 512 KB Output is correct
3 Correct 13 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 374 ms 3572 KB Output is correct
2 Correct 393 ms 3572 KB Output is correct
3 Correct 416 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 379 ms 3572 KB Output is correct
2 Correct 442 ms 3572 KB Output is correct
3 Correct 429 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 373 ms 3572 KB Output is correct
2 Correct 420 ms 3572 KB Output is correct
3 Correct 449 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 395 ms 3572 KB Output is correct
2 Correct 413 ms 3572 KB Output is correct
3 Correct 432 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 372 ms 3572 KB Output is correct
2 Correct 415 ms 3572 KB Output is correct
3 Correct 420 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 3572 KB Output is correct
2 Correct 396 ms 3572 KB Output is correct
3 Correct 420 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 390 ms 3572 KB Output is correct
2 Correct 383 ms 3572 KB Output is correct
3 Correct 414 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 377 ms 3572 KB Output is correct
2 Correct 426 ms 3572 KB Output is correct
3 Correct 418 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 3572 KB Output is correct
2 Correct 399 ms 3572 KB Output is correct
3 Correct 463 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 396 ms 3572 KB Output is correct
2 Correct 400 ms 3476 KB Output is correct
3 Correct 440 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 3572 KB Output is correct
2 Correct 431 ms 3572 KB Output is correct
3 Correct 452 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 399 ms 3572 KB Output is correct
2 Correct 416 ms 3572 KB Output is correct
3 Correct 445 ms 3572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 3572 KB Output is correct
2 Correct 420 ms 3572 KB Output is correct
3 Correct 450 ms 3572 KB Output is correct