QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#836194#9922. Mah-jongucup-team4435#TL 0ms3648kbC++202.3kb2024-12-28 17:18:142024-12-28 17:18:14

Judging History

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

  • [2024-12-28 17:18:14]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3648kb
  • [2024-12-28 17:18:14]
  • 提交

answer

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

using ll = long long;
using ld = long double;

#define all(a) begin(a), end(a)
#define len(a) int((a).size())

const int N = 9;
int p3[N];
vector<array<int, 8>> v;

int at(int mask3, int pos) {
    return mask3 / p3[pos] % 3;
}

void precalc() {
    p3[0] = 1;
    for (int i = 1; i < N; i++) {
        p3[i] = p3[i - 1] * 3;
    }

    for (int mask = 0; mask < p3[6]; mask++) {
        array<int, 8> cur{};
        for (int i = 0; i < 6; i++) {
            int x = at(mask, i);
            for (int j : {i, i + 1, i + 2}) {
                cur[j] += x;
            }
        }
        v.push_back(cur);
    }
}

void solve(int /* test_num */) {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<array<int, 8>> pref(n + 1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        pref[i + 1] = pref[i];
        pref[i + 1][a[i]]++;
    }

    vector<int> pref_mask3(n + 1);
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j < 8; j++) {
            pref_mask3[i] += p3[j] * (pref[i][j] % 3);
        }
    }

    int cnt[p3[8]];
    ll ans = 0;
    for (auto lim : v) {
        memset(cnt, 0, sizeof(cnt));

        int j = n;
        int start = n - 1;

        for (int x = 0; x < 8; x++) {
            while (start >= 0 && pref[j][x] - pref[start][x] < lim[x]) {
                start--;
            }
        }
        if (start < 0) {
            continue;
        }

        int mapping[p3[8]];
        memset(mapping, 0, sizeof(mapping));
        for (int mask3 = 0; mask3 < p3[8]; mask3++) {
            for (int i = 0; i < 8; i++) {
                mapping[mask3] += ((at(mask3, i) + lim[i]) % 3) * p3[i];
            }
        }

        cnt[pref_mask3[n]]++;
        for (int i = start; i >= 0; i--) {
            while (j > i + 1 && pref[j][a[j - 1]] - pref[i][a[j - 1]] > lim[a[j - 1]]) {
                cnt[pref_mask3[--j]]++;
            }
            ans += cnt[mapping[pref_mask3[i]]];
        }
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    precalc();

    int tests;
    cin >> tests;
    for (int test_num = 1; test_num <= tests; test_num++) {
        solve(test_num);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2
5
1
3
2

result:

ok 5 number(s): "2 5 1 3 2"

Test #2:

score: -100
Time Limit Exceeded

input:

100
992
8 1 8 1 2 3 6 6 1 3 1 8 7 7 4 7 7 1 6 6 4 8 3 7 3 5 1 4 4 7 5 7 5 7 4 3 7 5 2 8 7 1 6 3 6 2 4 3 2 3 1 6 3 1 3 2 6 6 7 4 6 1 1 4 6 4 7 7 8 5 6 4 1 5 4 8 2 4 4 2 1 3 5 7 6 8 3 7 6 6 5 6 4 2 5 4 3 7 3 5 5 3 3 2 7 8 2 7 2 4 4 3 4 1 1 3 5 5 4 6 3 3 3 2 6 1 2 6 4 8 8 6 6 8 7 3 1 1 8 8 7 2 5 6 3 5 ...

output:


result: