双向搜索 10 pts 求助

P4799 [CEOI2015 Day2] 世界冰球锦标赛

SilverLi @ 2023-07-05 11:54:15

#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int N = 45;
int n, w, v[N];
int sl[N], sr[N];
int ans, l, r, d;
void left(int i, int sum) {
    if (i == d + 1) {
        sl[++l] = sum;
        return;
    }
    if (sum + v[i] <= w)
        left(i + 1, sum + v[i]);
    left(i + 1, sum);
}
void right(int i, int sum) {
    if (i == n + 1) {
        sr[++r] = sum;
        return;
    }
    if (sum + v[i] <= w)
        right(i + 1, sum + v[i]);
    right(i + 1, sum);
}
#define all_sl sl + 1, sl + l + 1
signed main() {
    cin >> n >> w;
    for (int i = 1; i <= n; ++i)
        cin >> v[i];
    d = n / 2;
    left(1, 0);
    right(n - d, 0);
    sort(all_sl);
    sort(sr + 1, sr + r + 1);
    for (int i = 1; i <= r; ++i) {
        int k = upper_bound(all_sl, w - sr[i]) - sl;
        ans += k - 1;
    }
    cout << ans;
    return 0;
}

by dsfgsdf @ 2023-07-05 16:19:53

@NM_ljy 你太强了,以至于求助发出来,都没人会(9(6翻了))


by loser_wanghan @ 2024-02-15 20:04:23

@SilverLi 你这好像是折半搜索不是双向搜索


by 颗粒zhu @ 2024-05-14 18:06:07

十年OI一场空,不开long long见祖宗


|