CDOJ 1528 BG

题目链接 : CDOJ1528

 

BG

Time Limit: 5000 ms Memory Limit: 65536 kB Solved: 18 Tried: 192

Description

Wangkun has to treat his friend gleiz a meal for he has got an offer from PKU.

Wangkun has M yuan in his pocket and of course gleiz wants to spend all of them for the meal, and they want to order at least K dishes for the abundance of the cuisine.

Now you are shown the menu which gives you the prices of all the dishes in the restaurant. Your task is to figure out the number of plans that help gleiz spend all wangkun's money to fulfill his appetite!

Input

There are multiple cases.

The first line of a case contains 3 integers N and K and M(1 <= K <= N <= 38, 1 <= M <= 38*10^9), the number of dishes in the restaurant.

The second line of a case contains N integers P[1], P[2], ..., P[N] (1 <= P[i] <= 10^9), giving you the prices of the dishes respectively.

It is ensured that all the prices are distinct.

Output

For each case you should output the number of plans in a line.

Sample Input

5 1 5
5 4 3 2 1
4 2 3
1 2 4 8
4 2 1
1 2 4 8

Sample Output

3
1
0

Source

5th BUPT Programming Contest Final

二分数组~
 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;


int bag[50];
struct p
{
    long long sum;
    int dis;
}dp[530000],dp2[530000];
bool cmp(p a,p b)
{
    return a.sum>b.sum;
}
int main()
{
    freopen("1.txt","r",stdin);
    int n,k;
    long long m;
    while(scanf("%d%d%lld",&n,&k,&m)==3)
    {
        for(int i=0;i<n;i++)
            scanf("%d",bag+i);
        int lim=n/2,con;
        int a=1<<lim,b=1<<n-lim;
        long long sum;
        for(int i=0;i<a;i++)
        {
            sum=con=0;
            for(int j=0;j<lim;j++)
                if((1<<j)&i) sum+=bag[j],con++;
            dp[i].dis=con;dp[i].sum=sum;
        }
        int fa=lim;
        lim=n-lim;
        for(int i=0;i<b;i++)
        {
            sum=con=0;
            for(int j=0;j<lim;j++)
                if((1<<j)&i) sum+=bag[j+fa],con++;
            dp2[i].dis=con;dp2[i].sum=sum;
        }
        sort(dp,dp+a,cmp);
        sort(dp2,dp2+b,cmp);
        b--;
        int c=0;
        sum=0;lim=0;
        while(b>-1 && c<a)
        {
            if(dp[c].sum+dp2[b].sum>m) c++,lim=c;
            else if(dp[c].sum+dp2[b].sum<m) b--;
            else
            {
                if(dp[c].dis+dp2[b].dis>=k) sum++;
                if(c+1<a && dp[c+1].sum==dp[lim].sum) c++;
                else
                {
                    if(b && dp2[b-1].sum==dp2[b].sum) c=lim;
                    b--;
                }
            }
        }
        printf("%lld\n",sum);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值