QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270283#7753. Energy Distributionucup-team859#ML 927ms197784kbC++142.8kb2023-11-30 18:05:112023-11-30 18:05:11

Judging History

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

  • [2024-10-31 10:22:30]
  • hack成功,自动添加数据
  • (/hack/1089)
  • [2023-11-30 18:05:11]
  • 评测
  • 测评结果:ML
  • 用时:927ms
  • 内存:197784kb
  • [2023-11-30 18:05:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

using ll = long long;
using ull = unsigned long long;

string to_string(const string &s) {
  return '"' + s + '"';
}

string to_string(bool b) {
  return b ? "true" : "false";
}

template <typename A, typename B>
string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename T>
string to_string(const T &v) {
  string s = "{";
  bool first = true;
  for (const auto &it : v) {
    if (!first)
      s += ", ";
    else
      first = false;
    s += to_string(it);
  }
  return s += "}";
}

void debug_out() {
  cerr << endl;
}

template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
  cerr << to_string(first) << " ";
  debug_out(rest...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

auto startTime = high_resolution_clock::now();
int get_time() {
  auto stopTime = chrono::high_resolution_clock::now();
  auto duration = duration_cast<milliseconds>(stopTime - startTime);
  return duration.count(); // in ms
}

using ld = long double;

const ld eps = 1e-6;

int n;
int w[11][11];

ld eval(vector<ld> &p) {
  ld sum = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j)
      sum += p[i] * p[j] * w[i][j];
  }

  return sum;
}

void norm(vector<ld> &p) {
  ld sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += p[i];

  for (int i = 1; i <= n; ++i)
    p[i] /= sum;
}

ld run_regr(vector<ld> &p) {
  ld best = eval(p), df;

  int pos = -1;
  vector<ld> aux = p;
  for (int i = 1; i <= n; ++i) {
    p[i] += eps;
    norm(p);
    ld res = eval(p);
    if (res > best) {
      best = res;
      df = eps;
      pos = i;
    }
    p = aux;

    p[i] -= eps;
    if (p[i] >= 0) {
      norm(p);
      ld res = eval(p);
      if (res > best) {
        best = res;
        df = -eps;
        pos = i;
      }
    }
    p = aux;
  }

  if (pos == -1 || get_time() >= 950)
    return best;

  p[pos] += df;
  norm(p);
  return run_regr(p);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> w[i][j];
    }
  }

  vector<ld> p(n + 1, 0);
  ld best = 0;
  while (get_time() <= 900) {
    auto dis = uniform_real_distribution<ld>(0, 1);
    for (int i = 1; i <= n; ++i)
      p[i] = dis(rng);

    norm(p);
    best = max(best, run_regr(p));
  }
  cout << fixed << setprecision(20) << best << "\n";
}

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

  int t = 1;
//  cin >> t;
  while (t--)
    solve();

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 896ms
memory: 127420kb

input:

2
0 1
1 0

output:

0.24999999999999903574

result:

ok found '0.2500000', expected '0.2500000', error '0.0000000'

Test #2:

score: 0
Accepted
time: 927ms
memory: 145896kb

input:

3
0 2 1
2 0 2
1 2 0

output:

0.57142857142856607564

result:

ok found '0.5714286', expected '0.5714290', error '0.0000004'

Test #3:

score: 0
Accepted
time: 872ms
memory: 197784kb

input:

3
0 1 2
1 0 1
2 1 0

output:

0.49999999999995828096

result:

ok found '0.5000000', expected '0.5000000', error '0.0000000'

Test #4:

score: -100
Memory Limit Exceeded

input:

4
0 3 1 0
3 0 1 0
1 1 0 2
0 0 2 0

output:


result: