QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#752410#9540. Double 11hos_lyricWA 1ms3952kbC++144.7kb2024-11-16 02:10:212024-11-16 02:10:21

Judging History

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

  • [2024-11-16 02:10:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3952kb
  • [2024-11-16 02:10:21]
  • 提交

answer

// modified https://qoj.ac/submission/750888

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class T> struct MonotoneMinDP {
  vector<pair<T, int>> dp;
  vector<pair<int, int>> que;
  template <class Cost> pair<T, int> operator()(int n, Cost cost) {
    auto get = [&](int l, int r) -> pair<T, int> {
// assert(0<=l);assert(l<r);assert(r<=n);
// assert(0<=l);assert(l<=n);assert(0<=r);assert(r<=n);
      return make_pair(dp[l].first + cost(l, r), dp[l].second + 1);
    };
    dp.resize(n + 1);
    que.resize(n + 1);
    /*
    int head = 0, tail = 0;
    dp[0] = make_pair(T(), 0);
    for (int i = 1; i <= n; ++i) {
      dp[i] = get(0, i);
      while (head < (tail-1) && que[head + 1].second <= i) head++;
      if (head <= (tail-1)) {
        if (get(que[head].first, i) < dp[i]) dp[i] = get(que[head].first, i);
        while (head < (tail-1) && que[head + 1].second <= i + 1) head++;
        if (head <= (tail-1)) que[head].second = i + 1;
      }
      int r = n;
      while (head <= (tail-1)) {
        if (get(que[(tail-1)].first, que[(tail-1)].second) > get(i, que[(tail-1)].second)) r = que[(tail-1)].second - 1, --tail;
        else if (get(que[(tail-1)].first, r) <= get(i, r)) {
          if (r < n) que[tail++] = make_pair(i, r + 1);
          break;
        } else {
          int L = que[(tail-1)].second, R = r;
          while (L < R) {
            int M = (L + R) >> 1;
            if (get(que[(tail-1)].first, M) <= get(i, M)) L = M + 1;
            else
              R = M;
          }
          que[tail++] = make_pair(i, L);
          break;
        }
      }
      if (head > (tail-1)) {
        que[tail++] = make_pair(i, i + 1);
      }
    }
    */
    int head = 0, tail = 1;
    que[0] = make_pair(0, 1);
    for (int i = 1; i <= n; ++i) {
      for (; head + 1 < tail && que[head + 1].second <= i; ++head) {}
      dp[i] = get(que[head].first, i);
      for (; head + 1 < tail && que[head + 1].second <= i + 1; ++head) {}
      if (head < tail) que[head + 1].second = i + 1;
      for (int rr = n + 1; ; --tail) {
        if (head == tail) {
          que[tail++] = make_pair(i, i + 1);
          break;
        }
        const int l = que[tail - 1].first, r = que[tail - 1].second;
        if (get(l, r) < get(i, r)) {
          int lo = r, hi = rr;
          for (; lo + 1 < hi; ) {
            const int mid = (lo + hi) / 2;
            ((get(l, mid) < get(i, mid)) ? lo : hi) = mid;
          }
          if (hi <= n) que[tail++] = make_pair(i, hi);
          break;
        } else {
          rr = r;
        }
      }
    }
    return dp[n];
  }
};  // MonotoneMinDP


using Double = long double;

int N, M;
vector<Int> S, SSum;

MonotoneMinDP<Double> f;
pair<Double, int> calc(Double pena) {
  return f(N, [&](int l, int r) -> Double {
    return sqrt((Double)((r - l) * (SSum[r] - SSum[l]))) + pena;
  });
}

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    S.resize(N);
    for (int i = 0; i < N; ++i) scanf("%lld", &S[i]);
    sort(S.begin(), S.end());
    
    SSum.assign(N + 1, 0);
    for (int i = 0; i < N; ++i) SSum[i + 1] = SSum[i] + S[i];
    
    Double lo = -100, hi = 1e9;
    for (int iter = 0; iter < 60; ++iter) {
      const Double mid = (lo + hi) / 2;
      const auto res = calc(mid);
      ((res.second >= M) ? lo : hi) = mid;
    }
    const auto res = calc(lo);
    printf("%.12Lf\n", res.first - res.second * lo);
if(M==106845)puts("qwq");
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 2
1 2 3 4

output:

6.191147129557

result:

ok found '6.191147130', expected '6.191147130', error '0.000000000'

Test #2:

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

input:

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

output:

22.591625366514

result:

ok found '22.591625367', expected '22.591625367', error '0.000000000'

Test #3:

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

input:

1 1
1

output:

1.000000000000

result:

ok found '1.000000000', expected '1.000000000', error '0.000000000'

Test #4:

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

input:

1 1
100000

output:

316.227766016847

result:

ok found '316.227766017', expected '316.227766017', error '0.000000000'

Test #5:

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

input:

7 1
10 47 53 9 83 33 15

output:

41.833001326711

result:

ok found '41.833001327', expected '41.833001327', error '0.000000000'

Test #6:

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

input:

8 5
138 1702 119 1931 418 1170 840 1794

output:

233.901654551944

result:

ok found '233.901654552', expected '233.901654552', error '0.000000000'

Test #7:

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

input:

58 1
888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983

output:

1355.265287683578

result:

ok found '1355.265287684', expected '1355.265287684', error '0.000000000'

Test #8:

score: -100
Wrong Answer
time: 1ms
memory: 3900kb

input:

88 30
67117 31903 93080 85196 16438 97116 11907 72959 83651 41273 52873 81892 81468 51323 99992 58869 54258 7183 87358 90990 80596 41252 90769 82705 61434 8524 13575 10787 53950 96768 12062 34637 27806 70937 69653 28380 90236 3352 27537 3873 91006 89790 25369 91825 82734 5588 4539 74118 47098 84741 ...

output:

18794.550670223627

result:

wrong answer 1st numbers differ - expected: '18791.4753541', found: '18794.5506702', error = '0.0001637'