2017西安交大ACM小学期 选择困难症[折半枚举+二分查找]

选择困难症

发布时间: 2017年7月4日 12:44   最后更新: 2017年7月4日 12:45   时间限制: 5000ms   内存限制: 128M

GG有严重的选择困难症。

早上起床后,需要花很长时间决定今天穿什么出门。

假设一共有k类物品需要搭配选择,每类物品的个数为Ai,每个物品有一个喜欢值Vj,代表GG对这件物品的喜欢程度。

GG想知道,有多少种方案,使得选出来的总喜欢值>M

需要注意,每类物品,至多选择1件,可以不选。

多组输入
每组数据第一行输入k M(k<=6,1<=M<=1e8),表示有多少类物品
接下来k行,每行以Ai(1<=Ai<=100)开头,表示这类物品有多少个,接下来Ai个数,第j个为Vj(1<=Vj<=1e8),表示小L对这类物品的第j个的喜欢值是多少

每组输出一行,表示方案数

  复制
3 3
2 1 2
2 1 2
2 1 2
10

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long
const int MAX = 1e7+8;
int k,M;
int a[7][107];
int cnt[6];
int arr[MAX];
int brr[MAX];
int cnta = 0;
int cntb = 0;
void dfs(int array[],int& cntp,int step,int end,int sum){
	if(step == end){
		array[cntp++] = sum;
		return ;
	}
	for(int i = 0;i <= cnt[step];i++){
		dfs(array,cntp,step+1,end,sum + a[step][i]);
	}
	
}
main(){
	while(scanf("%lld%lld",&k,&M) != EOF){
		memset(cnt,0,sizeof(cnt));
		cnta = cntb = 0;
		for(int i = 0;i < k;i++){
			scanf("%lld",&cnt[i]);
			for(int j = 0;j < cnt[i];j++){
				scanf("%lld",&a[i][j]);
			}
			a[i][cnt[i]] = 0;
		}
		int mid = k / 2;
		int last = 0;
		dfs(arr,cnta,0,mid,0);
		dfs(brr,cntb,mid,k,0);
		sort(brr,brr + cntb);
		int ans = 0;
		for(int i = 0;i < cnta;i++){
			ans += brr + cntb - upper_bound(brr,brr + cntb,M - arr[i]);
		}
		printf("%lld\n",ans);
	}
	return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值