当前位置 : 主页 > 编程语言 > c++ >

折半搜索(超大背包)

来源:互联网 收集:自由互联 发布时间:2021-06-23
https://ac.nowcoder.com/acm/contest/889/D #includebits/stdc++.h using namespace std; // __ #define pb push_back typedef long long ll;typedef unsigned long long ull; struct node{ int num; ull sum;}a[ 1 19 ],b[ 1 19 ]; bool cmp(node p,node q)

https://ac.nowcoder.com/acm/contest/889/D

#include<bits/stdc++.h>
using namespace std;
//__
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
struct node{
    int num;
    ull sum;
}a[1<<19],b[1<<19];
bool cmp(node p,node q){
    return p.sum<q.sum;
}

int main(){
    int n;
    ull s;
    scanf("%d%llu",&n,&s);
    int lena=1,lenb=1;
    int p=n/2;
    for(int i=0;i<p;i++){
        ull x;
        scanf("%llu",&x);
        int m=lena;
        for(int j=0;j<m;j++){
            a[lena].num=(a[j].num|(1<<i));
            a[lena].sum=a[j].sum+x;
            lena++;
        }
    }
    for(int i=p;i<n;i++){
        ull x;
        scanf("%llu",&x);
        int m=lenb;
        for(int j=0;j<m;j++){
            b[lenb].num=(b[j].num|(1<<i));
            b[lenb].sum=b[j].sum+x;
            lenb++;
        }
    }
    sort(a,a+lena,cmp);
    sort(b,b+lenb,cmp);
    int l=0,r=lenb-1;
    while(l<lena){
        while(a[l].sum+b[r].sum>s&&r)
            r--;
        if(a[l].sum+b[r].sum==s)
            break;
        l++;
    }
    for(int i=0;i<(int)n/2;i++)
        printf("%d",(a[l].num>>i)&1);
    for(int i=int(n/2);i<n;i++)
        printf("%d",(b[r].num>>i)&1);
    return 0;
}
View Code
网友评论