标签:return push pac second targe code space def acm
超大背包
折半查找
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll,int> ll A[39]; ll C[39]; bool B[39]; ll s,_n; vector<P> v1,v2; void dfs(int i,ll x,int n,bool f) { P a; a.first=x; int t=0; if(f) for(int i=0; i<n; i++) { t=t*2+B[i]; } else { for(int i=_n/2; i<n; i++) { t=t*2+B[i]; } } a.second=t; if(f) v1.push_back(a); else v2.push_back(a); if(i==n)return ; else { B[i]=1; dfs(i+1,x+A[i],n,f); B[i]=0; dfs(i+1,x,n,f); } } int main() { scanf("%lld%lld",&_n,&s); for(int i=0; i<_n; i++) { scanf("%lld",&A[i]); } dfs(0,0,_n/2,1); dfs(_n/2,0,_n,0); sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); int n=v1.size();int m=v2.size(); for(int i=0;i<n;i++){ ll x=v1[i].first; ll t=s-x; int l=0,r=m-1; while(l+1<r){ //cout<<l<<r<<‘\n‘; int mid=(l+r)/2; if(v2[mid].first>t){ r=mid-1; }else if(v2[mid].first==t){ l=mid; break; } else { l=mid+1; } } // cout<<x<<" "<<v2[l].first<<‘\n‘; stack<int>st; if(v2[l].first==s-x){ int _=v1[i].second; for(int i=0;i<_n/2;i++){ st.push(_%2); _/=2; } while(!st.empty()){ cout<<st.top(); st.pop(); } _=v2[l].second; for(int i=_n/2;i<_n;i++){ st.push(_%2); _/=2; } while(!st.empty()){ cout<<st.top(); st.pop(); } break; } } }
标签:return push pac second targe code space def acm
原文地址:https://www.cnblogs.com/liulex/p/11359696.html