QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334060#8050. Random Permutationhos_lyricTL 2042ms5368kbC++142.7kb2024-02-21 05:34:412024-02-21 05:34:42

Judging History

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

  • [2024-02-21 05:34:42]
  • 评测
  • 测评结果:TL
  • 用时:2042ms
  • 内存:5368kb
  • [2024-02-21 05:34:41]
  • 提交

answer

#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")


const double L = 500;

int N;
vector<int> P;

int main() {
  for (; ~scanf("%d", &N); ) {
    P.assign(N + 2, 0);
    for (int i = 1; i <= N; ++i) {
      scanf("%d", &P[i]);
    }
    
    vector<int> invP(N + 2, 0);
    for (int i = 1; i <= N; ++i) invP[P[i]] = i;
    
    Int ans = 0;
Int sumM=0;
    for (int a = 1; a <= N; ++a) {
      /*
        interval of length m + 1
        binomial distribution
          Pr[small] = p
          E[# small] = m p
          V[# small] = m p (1 - p)
        E[large - small] = m (1 - 2 p)
        V[large - small] = 4 m p (1 - p)
        let 0 or +1 lie within m (1 - 2 p) +- L * 2 sqrt(m p (1 - p))
      */
      const double p = (a - 1) / (double)(N - 1);
      int m = L * 2 * sqrt(p * (1 - p)) / max(abs(1 - 2 * p), 1e-6);
      m = min(max(m, 1000), N);
sumM+=m;
      vector<int> freq(2 * m + 3, 0);
      int now = m + 1;
      ++freq[now];
      for (int i = invP[a] - 1; i >= 1 && i >= invP[a] - m; --i) {
        (P[i] < a) ? --now : ++now;
        ++freq[now];
      }
      Int here = 0;
      now = m + 1;
      here += freq[2 * m + 2 - now] + freq[2 * m + 3 - now];
      for (int i = invP[a] + 1; i <= N && i <= invP[a] + m; ++i) {
        (P[i] < a) ? --now : ++now;
        here += freq[2 * m + 2 - now] + freq[2 * m + 3 - now];
      }
// cerr<<a<<": "<<m<<" "<<here<<endl;
      ans += here * a;
    }
cerr<<"sumM = "<<sumM<<endl;
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 0
Accepted
time: 2042ms
memory: 5368kb

input:

100000
56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...

output:

250202478701074

result:

ok 1 number(s): "250202478701074"

Test #3:

score: -100
Time Limit Exceeded

input:

200000
70900 189146 39056 135530 191967 133789 108558 81993 132081 113826 54456 127761 27836 64897 87159 105191 109446 81230 75525 90396 75756 50200 43091 151358 100791 193998 157161 119352 176873 120724 134471 155040 138534 182263 161855 4577 124893 199710 60764 146460 75314 43696 129155 64816 1390...

output:


result: