ZOJ--3631--Watashi's BG【枚举】
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4777
题意:有n天,告诉你每天的花费,别人给你一笔资金m,你自己也有一部分资金(能够如果花不完),每天仅仅能花自己的钱或者花资金m中的钱,不能混着花,问m最多能花多少?
思路:考虑到数据比較小,n最多仅仅有30,能够用枚举来做,枚举每天花m或者不花m,二进制枚举,复杂度2^30。这样复杂度要超时的,土豪说能够分两部分枚举,把结果存入两个数组,然后这两个数组结果再合并,复杂度n^2,这样二进制枚举最多是两个2^15,大大缩短了时间复杂度。
后来我又用dfs写了一遍,果断T,看了别人的代码,有个剪枝非常关键,当前已使用的资金与全部还未使用的资金相加假设小于等于已得到的最大值,则剪枝。
二进制枚举代码
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 5000100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define MOD 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int a[200010],b[200010],va[40],vb[40];
int main(){
int i,j;
int n,m;
int la,lb,cnta,cntb,ans;
while(scanf("%d%d",&n,&m)!=EOF){
la = lb = cnta = cntb = ans = 0;
for(i=0;i<n/2;i++){
scanf("%d",&va[i]);
la++;
}
for(i=n/2;i<n;i++){
scanf("%d",&vb[i-n/2]);
lb++;
}
int l = (1<<la);
for(i=0;i<l;i++){
int sum = 0;
for(j=0;j<la;j++){
if(i&(1<<j)){
sum += va[j];
}
}
if(sum<=m){
a[cnta++] = sum;
ans = max(sum,ans);
}
}
l = (1<<lb);
for(i=0;i<l;i++){
int sum = 0;
for(j=0;j<lb;j++){
if(i&(1<<j)){
sum += vb[j];
}
}
if(sum<=m){
b[cntb++] = sum;
ans = max(sum,ans);
}
}
sort(a,a+cnta);
sort(b,b+cntb);
if(ans==m){
printf("%d\n",ans);
continue;
}
for(i=cnta-1;i>=0;i--){
for(j=cntb-1;j>=0;j--){
if(a[i]+b[j]<=m){
ans = max(ans,a[i]+b[j]);
break;
}
}
}
printf("%d\n",ans);
}
return 0;
}
DFS代码
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 5000100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define MOD 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int a[40],jz[40];
int n,m,maxm;
void dfs(int step,int cnt){
if(cnt>m) return ;
maxm = max(maxm,cnt);
if(step<1) return ;
if(cnt+jz[step]<=maxm) return ; //防TLE剪枝
dfs(step-1,cnt);
dfs(step-1,cnt+a[step]);
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
jz[0] = 0;
for(i=1;i<=n;i++) jz[i] = jz[i-1] + a[i];
maxm = 0;
dfs(n,0);
printf("%d\n",maxm);
}
return 0;
}
ZOJ--3631--Watashi's BG【枚举】的更多相关文章
- ZOJ 3631 Watashi's BG DFS
J - Watashi's BG Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- [ZOJ 3631] Watashi's BG
Watashi's BG Time Limit: 3 Seconds Memory Limit: 65536 KB Watashi is the couch of ZJU-ICPC Team ...
- HDU 4430 & ZOJ 3665 Yukari's Birthday(二分法+枚举)
主题链接: HDU:pid=4430" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4430 ...
- zoj 3356 Football Gambling II【枚举+精度问题】
题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...
- ZOJ 3587 Marlon's String 扩展KMP
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...
- zoj 1738 - Lagrange's Four-Square Theorem
称号:四方形定理.输出可以表示为一个数目不超过四个平方和表示的数. 分析:dp,完全背包.背包分割整数.可用一维分数计算,它也可以被写为一个二维团结. 状态:设f(i,j,k)为前i个数字,取j个数字 ...
- zoj 2156 - Charlie's Change
称号:拼布钱,表面值至1,5.10.25.寻求组成n表面值硬币的最大数目. 分析:dp,01背包.需要二元分割,除此以外TLE.使用每个硬币的数组记录数.轻松升级. 写了一个 多重背包的 O(NV)反 ...
- zoj 3696 Alien's Organ(泊松分布)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3696 Alien's Organ Time Limit: 2 S ...
- zoj 2402 - Lenny's Lucky Lotto Lists
称号:序列,在前面的每个元件的至少两倍,最大值至n.问:长l船舶有许多这样的. 分析:dp,LIS类别似事. 状态:f(i,j)结束数字为j且长度为i的序列的个数.有转移方程: F[ i ][ j ] ...
随机推荐
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- Linux统计文件/目录数量ls -l | grep "^-" | wc -l匹配开头和结尾
Linux统计文件数量 ls -l | grep "^-" | wc -l “^-” 一般文件 “^d” 目录文件 shell/vim中^表示开头 cat repatterns ...
- ThinkPhp学习09
原文:ThinkPhp学习09 三.区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是 and 的关系 //SELECT * FR ...
- [Django实战] 第5篇 - 用户认证(修改密码)
上一篇我们实现了用户认证系统的登录模块,这一篇实现修改密码模块. 同样地,我们首先得给修改密码创建表单(forms.py): class ChangepwdForm(forms.Form): oldp ...
- spring开发基础
Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途 ...
- [docker]coreOS与atomic对照
声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 摘自https://majo ...
- codeforces 598A Tricky Sum
题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
- [Android学习笔记]ScrollView的使用
竖直滚动使用ScrollView 水平滚动使用HorizontalScrollView 如果需要禁止ScrollView的滚动,则需要扩展ScrollView类,重写onTouchEvent方法.
- winform下载网页代码
1:webClient client=new WebClient(); client.Downloadstring(地址) client.Downloadfile(地址,保存路径) 2:后台线程dow ...
- schedule()函数的调用时机(周期性调度)
今天纠正了一个由来已久的认识错误:一个进程的时间片用完之后,当再次发生时钟中断时内核会调用schedule()来进行调度,把当前的进程上下文切出CPU,并把选定的下一个进程切换进来运行.我一直以为sc ...