ZOJ 3631 Watashi's BG(双向枚举)
来源:互联网 发布:什么是协同过滤算法? 编辑:程序博客网 时间:2024/04/30 11:05
Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.
There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi's money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group's lunch by themselves or use Watashi's money. Of course, the leader wants to spend Watashi's money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi's reward. Can you help him?
Input
The input contains multiple test cases ( no more than 50 test cases ).
In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi.
Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ith day. All these integers are no more than 10000000 and integers are seperated by a space.
Output
For each test case, output one line with an integer which is the maximum money group C can spend from Watashi's reward
Sample Input
3 108 4 5
Sample Output
9
题意:给你n和M,n个数,求n个数里挑出一个或多个数的和不超过M的最大值是多少。
题解:因为n<=30,我们二进制枚举前一半能组成的数,排序,在枚举后一半能组成的数,再二分查找两数相加不超过M的最大数。
#include<cstring>#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<vector>#include<algorithm>#define ll long long#define N 33555using namespace std;int n,m;int dp[40];int a[40];int sumA[N],l;void debug() { for(int i=0; i<l; i++) printf("%d ",sumA[i]); cout<<endl;}void solve() { int nw=n/2; n=n-nw; l=0; for(int i=0; i<(1<<nw); i++) { int j=0; int x=i; int s=0; while(x) { if(x&1) { s+=a[j]; } x>>=1; j++; } sumA[l++]=s; } sort(sumA,sumA+l); //debug(); int ans=0; for(int i=0; i<(1<<n); i++) { int j=nw; int x=i; int s=0; while(x) { if(x&1)s+=a[j]; x>>=1; j++; } //printf("%d ",s); int sh=m-s; if(sh>=0) { int id=lower_bound(sumA,sumA+l,sh)-sumA; if(id==l)id--; if(sumA[id]>sh) id--; // printf("id=%d\n",id); if(id>=0) ans=max(ans,sumA[id]+s); } } cout<<ans<<endl;}int main() { // freopen("test.in","r",stdin); while(cin>>n>>m) { for(int i=0; i<n; i++) { scanf("%d",a+i); } if(n==1) { printf("%d\n",a[0]<=m?a[0]:0); continue; } solve(); } return 0;}
- ZOJ 3631 Watashi's BG(双向枚举)
- ZOJ--3631--Watashi's BG【枚举】
- zoj 3631 Watashi's BG(搜索)
- zoj - 3631 - Watashi's BG(dfs)
- ZOJ 3631 Watashi's BG
- zoj 3631 Watashi's BG
- ZOJ 3631 Watashi's BG
- zoj 3631 Watashi's BG 双向暴力搜素
- ZOJ 3631-Watashi's BG(bfs+循环队列)
- ZOJ 3631 Watashi's BG(DFS)
- ZOJ 3631 Watashi's BG (状态dp)
- 【DFS+剪枝】-ZOJ-3631-Watashi's BG
- ZOJ-#3631 Watashi's BG(DFS+剪枝)
- ZOJ 3631 Watashi's BG DFS
- TOJ 4367 ZOJ 3631 Watashi's BG / 背包 深搜
- ZJU 3631 - Watashi's BG
- Watashi's BG (深搜)
- zoj3631 Watashi's BG DFS
- 不重启VMWare虚拟机添加虚拟磁盘的方法(上)
- 0720linux共享库和静态库制作
- html中字符转义
- what is the film faced plywood
- FZU 2020 组合(Lucas定理)
- ZOJ 3631 Watashi's BG(双向枚举)
- 《IOS编程》控制逻辑课后练习 学习手记
- SpringMVC 用注解Annotation驱动的IoC功能@Autowired @Component
- 初识数据库
- Gym 100712G Heavy Coins(DFS)
- “儿童”计算器
- Gym 100712I Bahosain and Digits(枚举)
- Majority Number II
- 原型模式