Submission #23374649


Source Code Expand

import std.stdio, std.algorithm, std.conv, std.string, std.array, std.math;

double epsilon = 0.0000001;

double lostval(double[] a, double x)
{
  return a.fold!((acc, v) => acc + v + x - min(v, 2.0 * x))(0.0) / a.length;
}

double d_lostval(double[] a, double x)
{
  return a.fold!((acc, v) => acc - (v - 2.0 * x) / abs(v - 2.0 * x))(0.0);
}

double solve(double[] a, double bad, double good)
{
  int count = 100;

  while (count--) {
    double mid = (bad + good) / 2;
    double d = d_lostval(a, mid);
    while (d.isNaN) {
      mid += epsilon;
      d = d_lostval(a, mid);
    }
    if (d < 0)
      bad = mid;
    else
      good = mid;
  }
  return good;
}

void main()
{
  int n;
  readf!" %d "(n);
  auto a = readln.strip.split.map!(to!double).array;
  double min_x = 0.0;
  double max_x = a.maxElement;

  double x = solve(a, min_x, max_x);
  writefln("%.18f", lostval(a, x));
}

Submission Info

Submission Time
Task B - Insurance
User ssvb
Language D (LDC 1.20.1)
Score 500
Code Size 933 Byte
Status AC
Exec Time 46 ms
Memory 6620 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 16
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt
All 00-sample-001.txt, 00-sample-002.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 3 ms 3236 KiB
00-sample-002.txt AC 5 ms 3320 KiB
01-001.txt AC 4 ms 3112 KiB
01-002.txt AC 4 ms 3144 KiB
01-003.txt AC 4 ms 3172 KiB
01-004.txt AC 35 ms 5644 KiB
01-005.txt AC 23 ms 4240 KiB
01-006.txt AC 25 ms 4200 KiB
01-007.txt AC 18 ms 4128 KiB
01-008.txt AC 37 ms 5644 KiB
01-009.txt AC 43 ms 6480 KiB
01-010.txt AC 46 ms 6492 KiB
01-011.txt AC 39 ms 6476 KiB
01-012.txt AC 44 ms 6620 KiB
01-013.txt AC 44 ms 6460 KiB
01-014.txt AC 46 ms 6508 KiB