萌新求调教 SubTask1 RE 其他均AC

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

Qerrj @ 2023-10-19 10:58:23

#include<cstdio>
#include<vector> 
#include<algorithm>
using namespace std;
typedef long long lint;
const int N = 45;
lint read () {
    lint x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar(); 
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
lint n, mid, cntl, cntr, a[N], M;
vector<lint> l, r;
void ldfs (lint k, lint cost) {
//  printf("%d %lld\n", k, cost);
    if (cost > M) return; 
    if (k == mid) {
        l.push_back(cost);  
        return;
    }
    ldfs (k + 1, cost + a[k]);
    ldfs (k + 1, cost);
    return;
}
void rdfs (lint k, lint cost) {
//  printf("%d %lld\n", k, cost);
//  r.push_back(cost);
    if (cost > M) return;
    if (k == n + 1) {
        r.push_back(cost);
        return;
    }
    rdfs (k + 1, cost + a[k]);
    rdfs (k + 1, cost);
    return; 
}
lint ans;
int main() {
//  scanf("%lld%lld", &n, &m);
    n = read(), M = read();
//  printf("%d", M);
    mid = n / 2;
//  printf("%d\n", mid);
    for (int i = 1; i <= n; i++) a[i] = read();
    sort (a + 1, a + 1 + n);
    ldfs (1, 0); rdfs (mid, 0);
    sort (l.begin(), l.end()); 
    for (int i = 0; i < r.size(); i++) {
        if (M - r[i] < 0) continue; 
        ans += upper_bound(l.begin(), l.end(), M - r[i]) - l.begin();
    }
    printf("%lld", ans);
    return 0;
}

by louisliang @ 2023-10-19 11:11:30

@jsydsg

你前一半搜索少搜了一位,后一半搜索多了一位,会导致l没有数,改了就过了

AC link


by Qerrj @ 2023-10-19 11:29:46

@louisliang 感谢 确实是那里出了问题, 但我应该没有少搜, 只是把mid给了后面的那个dfs, 可能是mid = 0 时但前一位dfs从1开始 导致不断dfs直到爆栈, 所以才RE的吧 把等于号改成大于应该就对了


|