QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#701759#9540. Double 11ucup-team008#WA 4996ms5404kbC++175.2kb2024-11-02 14:40:362024-11-02 14:40:40

Judging History

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

  • [2024-11-02 14:40:40]
  • 评测
  • 测评结果:WA
  • 用时:4996ms
  • 内存:5404kb
  • [2024-11-02 14:40:36]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;

void solve() {
  auto srct = clock();
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  vector<ll> psum(n+1);
  auto eval = [&](int lhs, int rhs) -> double {
    // inclusive
    double sum = psum[rhs+1] - psum[lhs];
    return sqrt(sum * (rhs - lhs + 1));
  };
  for(auto& x: v) cin >> x;
  sort(all(v));
  for(int i = 0; i < n; i++) psum[i+1] = psum[i] + v[i];
  if(k == 1) {
    return void(cout << fixed << setprecision(39) << eval(0, n-1) << "\n");
  }
  vector<array<int, 2>> groups;
  vector<int> endpoints(n);
  iota(all(endpoints), 0);
  mt19937 g1(0x14004);
  double finans = 1e39;
  while(clock() - srct < 5 * CLOCKS_PER_SEC) {
    shuffle(all(endpoints), g1);
    endpoints.erase(endpoints.begin() + k-1, endpoints.end());
    sort(all(endpoints));
    for(int i = 0; i < k-1; i++) {
      groups.pb({i == 0 ? 0 : endpoints[i-1]+1, endpoints[i]});
    }
    groups.pb({endpoints[k-2]+1, n-1});
    while(true) {
      bool upd = false;
      for(int i = 0; i+1 < k; i++) {
        int lhs = groups[i][0];
        int rhs = groups[i+1][1]-1;
        int ans = -1;
        double best = 1e39;
        while(lhs <= rhs) {
          if(lhs == rhs) {
            int aa = groups[i][0];
            int ab = lhs;
            int ba = lhs+1;
            int bb = groups[i+1][1];
            double cand = eval(aa, ab) + eval(ba, bb);
            if(updmin(best, cand)) {
              ans = lhs;
            }
            break;
          }
          int a = lhs + (rhs - lhs) / 2;
          int b = a+1;
          double canda = eval(groups[i][0], a) + eval(a+1, groups[i+1][1]);
          double candb = eval(groups[i][0], b) + eval(b+1, groups[i+1][1]);
          if(canda < candb) {
            if(updmin(best, canda)) ans = a;
            rhs = a-1;
          }
          else {
            if(updmin(best, candb)) ans = b;
            lhs = b+1;
          }
        }
        if(ans != groups[i][1]) {
          groups[i][1] = ans;
          groups[i+1][0] = ans+1;
          upd = true;
        }
      }
      if(!upd) break;
    }
    double ret = 0;
    for(auto [a, b]: groups) {
      ret += eval(a, b);
      // for(int i = a; i <= b; i++) cerr << v[i] << " "; cerr << endl;
    }
    updmin(finans, ret);
  }
  cout << fixed << setprecision(39) << finans << "\n";
}

// what would chika do
// are there edge cases?
// did you actually sort the thing instead of just thinking it?
// integer overflow?

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

详细

Test #1:

score: 100
Accepted
time: 4996ms
memory: 4332kb

input:

4 2
1 2 3 4

output:

6.191147129557119654919006279669702053070

result:

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

Test #2:

score: 0
Accepted
time: 4988ms
memory: 5404kb

input:

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

output:

22.591625366514129780171060701832175254822

result:

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

Test #3:

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

input:

1 1
1

output:

1.000000000000000000000000000000000000000

result:

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

Test #4:

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

input:

1 1
100000

output:

316.227766016837961160490522161126136779785

result:

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

Test #5:

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

input:

7 1
10 47 53 9 83 33 15

output:

41.833001326703779909621516708284616470337

result:

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

Test #6:

score: -100
Wrong Answer
time: 4996ms
memory: 5316kb

input:

8 5
138 1702 119 1931 418 1170 840 1794

output:

234.081411261266737255937187001109123229980

result:

wrong answer 1st numbers differ - expected: '233.9016546', found: '234.0814113', error = '0.0007685'