选择困难症
发布时间: 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;
}