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 ] ...
随机推荐
- JSTL解析——002——core标签库01
javaEE5之前的版本需要引用JSTL相关的jar包.tld文件等,JAEE5之后就不用这么麻烦了, 如果你的还是不能使用就去官网下载(jstl.jar和standard.jar)这两个jar包,将 ...
- ecshop 微信支付插件
眼下已完毕支付測试,可以支付成功,支付逻辑自己实现.后台通知.发货通知.订单查询未測. 当中用到了redis 下载
- go之匿名字段
struct,定义的时候是字段名与其类型一一对应,实际上Go支持只提供类型,而不写字段名的方式,也就是匿名字段,也称为嵌入字段. 当匿名字段是一个struct的时候,那么这个struct所拥有的全部字 ...
- vs2008编译QT开源项目三国杀(五篇文章)
请参看 http://tieba.baidu.com/f?kz=1508964881 按照上面的网址教程,下载三国杀源码,swig工具,并下载最新的QT4.8.2 for vs2008.我本机已经安装 ...
- IOT表优缺点
<pre name="code" class="html">IOT表是将所有东西都塞到叶块中,表就是索引,可以避免回表 首先,对于IOT而言,只有索 ...
- Bootstrapping (compilers) - Wikipedia, the free encyclopedia
Bootstrapping (compilers) - Wikipedia, the free encyclopedia Bootstrapping (compilers)
- 如何使用git
本文不是谈论git具体命令的技术文章. 原文地址:http://blog.csdn.net/ffb/article/details/11206067 我之前发了一条关于git中如何处理中文文件名的微博 ...
- UITableView性能优化及手工绘制UITableViewCell
提高表视图的性能 UITableView作为应用中最常用的视图,它的性能优化问题几乎是经常提及.下面对在非网络访问情况下的表视图性能优化进行了主要的几点说明: 1.自定义类或XIB文件时 在系统提供的 ...
- 使用CocoaPods出现 The `master` repo requires CocoaPods 0.32.1 - 问题解决
近期在使用CocoaPods为project配置第三方类库时出现了例如以下问题: [!] The `master` repo requires CocoaPods 0.32.1 - 明显是由于Coco ...
- [gkk]传智-适配器设计模式,如同电源适配器
//适配器设计模式 是图形化设计中用的.如同电源适配器 import java.awt.*; inport java.awte public calss MyFrame{ public static ...