• Educational Codeforces Round 32 E 巨型背包


    思路:n只有35, 将n份为2部分,一部分为前n/2个物品的取舍(取或不去), 另一部分为剩下物品的取舍,复杂度为2^(n/2),枚举左边的数,然后二分右边的数找到最优解,写lower_bound需要去重,手写二分就不需要了

    AC代码:

    #include "iostream"
    #include "iomanip"
    #include "string.h"
    #include "stack"
    #include "queue"
    #include "string"
    #include "vector"
    #include "set"
    #include "map"
    #include "algorithm"
    #include "stdio.h"
    #include "math.h"
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
    #define mem(a,x) memset(a,x,sizeof(a))
    #define step(x) fixed<< setprecision(x)<<
    #define mp(x,y) make_pair(x,y)
    #define pb(x) push_back(x)
    #define ll long long
    #define endl ("
    ")
    #define ft first
    #define sd second
    #define lrt (rt<<1)
    #define rrt (rt<<1|1)
    using namespace std;
    const ll mod=1e9+7;
    const ll INF = 1e18+1LL;
    const int inf = 1e9+1e8;
    const double PI=acos(-1.0);
    const int N=1e5+100;
    
    int a[N],ans;
    vector<int> vex1, vex2;
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; ++i){
            scanf("%d",&a[i]);
            a[i]%=m;
        }
        int p=n>>1, q=n-p;
        for(int i=0; i<(1<<p); ++i){
            int u=0;
            for(int j=0; j<p; ++j){
                if(i&(1<<j)) u=(u+a[j+1])%m;
            }
            vex1.pb(u%m);
        }
        for(int i=0; i<(1<<q); ++i){
            int u=0;
            for(int j=0; j<q; ++j){
                if(i&(1<<j)) u=(u+a[j+p+1])%m;
            }
            vex2.pb(u%m);
        }
        sort(vex1.begin(), vex1.end()), unique(vex1.begin(), vex1.end());
        sort(vex2.begin(), vex2.end()), unique(vex2.begin(), vex2.end());
        for(auto u : vex1){
            int g=m-u;
            int it = lower_bound(vex2.begin(),vex2.end(),g) - vex2.begin()-1;
            if(it<0) continue;
            ans=max(ans, u+vex2[it]);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    操作系统简介
    正则表达式
    Python socket
    计算机网络基础
    计算机基础
    Python常用模块
    Python模块和包
    Python反射和内置方法(双下方法)
    Python类的成员
    更改命令行,完全显示hostname
  • 原文地址:https://www.cnblogs.com/max88888888/p/7821835.html
Copyright © 2020-2023  润新知