CH2401 送礼物(算竞进阶习题)
双向dfs
数据不是很大,但是如果直接暴搜的话2^45肯定过不了的。。
所以想到乱搞!!要让程序跑的更快,肯定要减下搜索树的规模,再加上这道题双搜的暗示比较明显(逃),所以就来乱搞+双搜求解
所以先从1~n/2(我的电脑测出来是n/2+2最快)开始枚举所有可能的重量,放进数组。。
这里注意,一定要先按降序排列,减小搜索树的规模,不然倒数第二个点死活过不去。。
然后再搜后半段,当后半段枚举到可能的答案t时,我们在之前的前半段可能组合的重量里二分查找W-t的前驱,这样就能组合成更大的结果来更新答案了
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C yql){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
return ans;
}
const int N = 50;
ll w, n, a[N], b[8400000], tot, ans, mid;
bool cmp(ll x, ll y){ return x > y; }
/*ll find(ll x){
ll l = 0, r = tot;
while(l < r){
ll mid = (l + r + 1) >> 1;
if(b[mid] <= x) l = mid; else r = mid - 1;
}
return b[l];
}*/
void dfs1(ll cur, ll sum){
if(cur == mid){
b[tot++] = sum;
return;
}
dfs1(cur + 1, sum);
if(sum + a[cur] <= w)
dfs1(cur + 1, sum + a[cur]);
}
void dfs2(ll cur, ll sum){
if(cur == n + 1){
ll k = lower_bound(b, b + tot, w - sum, greater<int>()) - b;
ans = max(ans, sum + b[k]);
return;
}
dfs2(cur + 1, sum);
if(sum + a[cur] <= w)
dfs2(cur + 1, sum + a[cur]);
}
int main(){
ios::sync_with_stdio(false);
//scanf("%lld%lld", &w, &n);
cin >> w >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
sort(a, a + n, cmp);
mid = n / 2 + 2;
dfs1(1, 0);
sort(b, b + tot, cmp); //reverse(b, b + tot);
tot = unique(b, b + tot) - b;
dfs2(mid, 0);
cout << ans << endl;
return 0;
}