QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#668202#8050. Random Permutationucup-team4992TL 1ms7692kbC++141.8kb2024-10-23 12:26:112024-10-23 12:26:12

Judging History

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

  • [2024-10-23 12:26:12]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7692kb
  • [2024-10-23 12:26:11]
  • 提交

answer

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


using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

static const int K = 100;
static const int maxn = 3e5 + 10;
static const int ZERO = 3e5 + 1;
int cntr[2][maxn << 1];

void solve() {
    ll ans = 0;
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++) {
        // for (int i = -n; i <= n; i++) {
        //     cout << cntr[0][ZERO + i] << ' ';
        // }
        // cout << '\n';
        int r = 1;
        int lsum = 0, rsum = 0;
        ll res = 0;
        cntr[i & 1][ZERO]++;
        while (abs(lsum - rsum) < K && r < max(i, n - i + 1)) {
            r++;
            if (i - r + 1 >= 1) {
                lsum += (a[i - r + 1] > a[i] ? 1 : -1);
            }
            if (i + r - 1 <= n) {
                rsum += (a[i + r - 1] > a[i] ? 1 : -1);
                cntr[(i + r - 1) & 1][ZERO + rsum]++;
            }
        }
        int L = max(1, i - r + 1), R = min(n, i + r - 1);
        // cout << L << ' ' << R << ' ' << lsum << ' ' << rsum << '\n';
        while (L <= i) {
            res += ll(a[i]) * cntr[L & 1][ZERO - lsum];
            res += ll(a[i]) * cntr[(L & 1) ^ 1][ZERO - lsum + 1];
            lsum -= (a[L] > a[i] ? 1 : -1);
            L++;
        }
        while (R >= i) {
            cntr[R & 1][ZERO + rsum]--;
            rsum -= (a[R] > a[i] ? 1 : -1);
            R--;
        }
        // cout << res << '\n';
        ans += res;
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);

    int T = 1;
    while (T--)
        solve();

    return 0;
}
/*
4
1 4 2 3
*/

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 7692kb

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: -100
Time Limit Exceeded

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:


result: