AtCoder - abc184_f

时限2s

Description:

Takahashi will participate in a programming contest, which lasts for T minutes and presents N problems.
With his extrasensory perception, he already knows that it will take Ai minutes to solve the i-th problem.
He will choose zero or more problems to solve from the N problems so that it takes him no longer than T minutes in total to solve them.
Find the longest possible time it takes him to solve his choice of problems.

1 ≤ N ≤ 40 ,1 ≤ Ai,T ≤ 1e9 

输入:

样例1

5 17
2 3 5 7 11

样例2

6 100
1 2 7 5 8 10

样例3

6 100
101 102 103 104 105 106

样例4

7 273599681
6706927 91566569 89131517 71069699 75200339 98298649 92857057

输出:

样例1

17

样例2

33

样例3

0

样例4

273555143

题目大意:给n个数,选若干数,使得和最大且不超过T

思路:折半搜索+二分

一开始想着背包,不过1e9的数据很难受,一直在想怎么优化,后来看了就40个数,时限2s,那铁暴力了

注意dfs会tle,2^40

n在20内时,状压一波然后暴力就行

n大于20时,分两半处理,把前20个数的取法所以状态下的和存起来,排序,计算后面20个数状态的时候,记后面20个数某种取法的和为temp, 在k-temp的限定下,去前面状态里二分找符合条件的最大值

最后别忘了开longlong

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define pb push_back
#define IOS ios::sync_with_stdio(false)
#define int long long
#define all(v) v.begin(),v.end()
#define INF 0x3f3f3f3f
typedef long long ll;
int n,k;
ll res;
int a[50];
vector <int > state;
bool check(int mid, int p)
{
    if( state[mid] <= p ) return true;
    return false;
}
signed main()
{
    ///!!!
//    freopen("data.txt","r",stdin);
    ///!!!
    IOS;
    cin>>n>>k;
    _for(i,1,n) cin>>a[i];

    if( n<=20)//小于20直接算就行
    {
            for(int i=0 ;i<=(1<<n)-1 ;i++)
        {
            ll temp=0;
            for(int j=(n-1);j>=0;j--)
            {
                if( (i>>j) & 1)
                {
                    temp += a[n-j];
                }
            }
            if(temp <= k )
            {
                res =max( res ,temp);
            }
        }
        cout<<res<<endl;
    }
    else//大于20,需要分两半算
    {
        for(int i=0 ;i<=(1<<20)-1 ;i++)
        {
            ll temp=0;
            for(int j=(n-1);j>=0;j--)
            {
                if( (i>>j) & 1)
                {
                    temp += a[n-j];
                }
            }
            if( temp <= k)
            {
                state.pb(temp);
            }
        }
        //后面要二分,先排序
        sort(all(state));
        for(int i=0 ;i<=(1<<n-20)-1 ;i++)
        {
            ll temp=0;
            for(int j=(n-1);j>=0;j--)
            {
                if( (i>>j) & 1)
                {
                    temp += a[n-20-j];
                }
            }
            if( temp <= k)
            {
                int l=0 ,r=state.size()-1;
                while( l<=r )//二分
                {
                    int mid=(l+r)>>1;
                    if( check(mid , k-temp) ) l=mid+1;
                    else r = mid-1;
                }
                //更新最大值
                res = max( res , temp + state[r]);
            }
        }
        cout<<res<<endl;
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值