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 ] ...
随机推荐
- [读书笔记]设计原本[The Design of Design]
第1章 设计之命题 1.设计首先诞生于脑海里,再慢慢逐步成形(实现) 2.好的设计具有概念完整性:统一.经济.清晰.优雅.利落.漂亮... 第2章 工程师怎样进行设计思维——理性模型 1.有序模型的有 ...
- 忽然想到:把Mu的源代码一网打尽
那么那些流媒体开发的公司,就不会拒绝我了,真是一举两得.
- 窗体的Alpha通道透明色支持(一旦 Form 被定义为利用 LayeredWindow ,窗口的绘图不再响应沿用多年的 WM_Paint 消息)
参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...
- 【IOS实例小计】打开google地图-web
-(void)openMaps:(id)sender{ NSString *addressText = @"1 Queen st, Auckland,NZ"; addressTex ...
- Python的经典问题——中文乱码
关键字:Python UTF-8 GBK 中文 乱码 估计入门时都会遇到的.我是在windows下用的Python25自带的IDLE编辑运行的,发现运行脚本得出的结果有一些中文显示是乱码,但有一些是正 ...
- The tempfile module
The tempfile module The tempfile module This module allows you to quickly come up with unique names ...
- ffmpeg 频中分离 video audio 截取片断
1.获取视频的信息 ffmpeg -i video.avi 2,将图片序列分解合成视频 ffmpeg -i src.mpg image%d.jpg ffmpeg -f image2 -i ...
- 实现ios常见菜单效果的思路
眼下见过的实现边側菜单的效果.比較流行的有下面三种:(效果图) 1.菜单条覆盖在部分主视图上 附上实现该效果的一个不错的源代码地址: http://code4app.com/ios/RNFrosted ...
- 在MVC应用程序中动态加载PartialView
原文:在MVC应用程序中动态加载PartialView 有时候,我们不太想把PartialView直接Render在Html上,而是使用jQuery来动态加载,或是某一个事件来加载.为了演示与做好这个 ...
- TopCoder SRM 625 Incrementing Sequence 题解
本题就是给出一个数k和一个数组,包含N个元素,通过每次添加�数组中的一个数的操作,最后须要得到1 - N的一个序列,不用排序. 能够从暴力法入手,然后优化. 这里利用hash表进行优化,终于得到时间效 ...