QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#458356#8831. Chemistry Classucup-team956#TL 0ms3528kbC++202.9kb2024-06-29 16:49:012024-06-29 16:49:05

Judging History

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

  • [2024-06-29 16:49:05]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3528kb
  • [2024-06-29 16:49:01]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x)
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

const int INF = 0x3f3f3f3f;

void solve() {
    int n; ll A, B;
    cin >> n >> A >> B;
    vector a(2 * n + 1, 0ll);
    for (int i = 1; i <= 2 * n; i++) {
        cin >> a[i];
    }
    sort(a.begin() + 1, a.begin() + 2 * n + 1);

    for (int i = 1; i <= 2 * n; i += 2) {
        if (a[i + 1] - a[i] > A) {
            cout << -1 << '\n';
            return ;
        }
    }

    vector sum(2, vector(2 * n + 1, 0));
    for (int i = 2; i <= 2 * n; i++) {
        sum[i & 1][i] = sum[i & 1][i - 2] + (a[i] - a[i - 1] <= B ? 1 : 0);
    }

    vector f(2 * n + 1, vector(2, -INF));
    f[0][0] = 0;
    // brute force DP
    for (int i = 2; i <= 2 * n; i++) {
        f[i][0] = f[i - 2][0] + (sum[i & 1][i] - sum[i & 1][i - 2]);
        for (int j = 1; j < i - 1; j++) {
            if (!((i ^ j) & 1)) {
                continue;
            }
            if (a[i] - a[j] > A) {
                continue;
            }
            f[i][1] = max(f[i][1], max(f[j - 1][0], f[j - 1][1]) + sum[j & 1][i - 1] - sum[j & 1][j]);
        }
    }
    
    // auto calc = [&](int p) -> int {
    //     return max(f[p - 1][0], f[p - 1][1]) - sum[p & 1][p];
    // };

    // deque<int> q0, q1;
    // q1.push_back(1);
    // for (int i = 2; i <= 2 * n; i++) {
    //     f[i][0] = f[i - 2][0] + (sum[i & 1][i] - sum[i & 1][i - 2]);
    //     while (!q0.empty() && a[i] - a[q0.front()] > A) {
    //         q0.pop_front();
    //     }
    //     while (!q1.empty() && a[i] - a[q1.front()] > A) {
    //         q1.pop_front();
    //     }

    //     if ((i & 1) && q0.empty()) {
    //         //
    //     } else if (!(i & 1) && q1.empty()) {
    //         //
    //     } else {
    //         int j = (i & 1) ? q0.front() : q1.front();
    //         f[i][1] = calc(j) + sum[j & 1][i - 1];
    //     }

    //     if (i & 1) {
    //         while (!q1.empty() && calc(q1.back()) < calc(i)) {
    //             q1.pop_back();
    //         }
    //         q1.push_back(i);
    //     } else {
    //         while (!q0.empty() && calc(q0.back()) < calc(i)) {
    //             q0.pop_back();
    //         }
    //         q0.push_back(i);
    //     }
    // }
    
    int ans = max(f[2 * n][0], f[2 * n][1]);
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 2 1
42 69
2 3 1
1 2 3 4
2 5 1
6 1 3 4
5 19 1
1 7 8 9 10 11 12 13 14 20

output:

-1
2
1
4

result:

ok 4 number(s): "-1 2 1 4"

Test #2:

score: -100
Time Limit Exceeded

input:

1
199996 67013419502794 1
403716252634677166 895717933735068492 410002430455111886 844431179242134559 322988383133810700 133475121268220299 481706326769800263 606871141911985391 195911124687409946 959578180866483093 930547702157856949 877914383714875160 994158366044742636 890855755285236186 69498488...

output:


result: