【洛谷 - U43391】不是0-1背包的暴力AC(思维,二分,可转化为二元组问题,复习暴力dfs总结)

题干:

https://www.luogu.org/problemnew/show/U43391

自01背包问世之后,小A对此深感兴趣。一天,小A去远游,却发现他的背包不同于01背包。 小A的背包最多能装W的价值
现有n件物品,分别为v1,v2,v3……vn
问如何存放物品,使背包内物品总价值达到最大

输入输出格式

输入格式:

 

输入第一行包含两个整数,分别代表n和W。 以后N行,每行一个正整数vi 表示第i个物品的价值 ​

 

输出格式:

 

输出仅一个整数,即最大价值。

 

输入输出样例

输入样例#1: 复制

5 10
1
3
4
8
9

输出样例#1: 复制

10

说明

1 <= n <= 40
1 <= vi <= 1e7
1 <= w <= 1e9

解题报告:

   这题显然是背包问题,但是就是没法用背包做。直接暴力加点剪枝就可以过了,而且跑的很快,毕竟数据水、、并且题目说了是暴力AC。

   但是正解是什么呢?很玄学、、把n从中间分成两份,然后答案就是三种情况:要么都在第一份,要么都在第二份,要么在第一份和第二份中各一个。很类似那道【CodeForces - 799C】Fountains  的做法。这里给出syt学长的代码。

暴力代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[55];
int n,m,loc;
ll ans = 0;void dfs(int x,ll cur) {if(x == loc) {ans = max(ans,cur);return;}ans = max(ans,cur);for(int i = x+1; i<=loc; i++) {if(cur+a[i] <= m)dfs(i,cur+a[i]);}
}
int main()
{cin>>n>>m;for(int i = 1; i<=n; i++) scanf("%lld",a+i);sort(a+1,a+n+1);for(int i = 1; i<=n; i++) {loc = i;if(a[i] > m) break;}dfs(1,0);if(a[1] <= m)dfs(1,a[1]);printf("%lld\n",ans);return 0 ;} 

其中dfs函数也可以这么写:

void dfs(int x,ll cur) {//代表截止当前(x)位置,价值为cur的状态。 ans = max(ans,cur);if(x == loc) return;for(int i = x+1; i<=loc; i++) {if(cur+a[i] <= m) dfs(i,cur+a[i]);}
}

或者这样:

void dfs(int x,ll cur) {if(x == loc) {ans = max(ans,cur);return;}if(cur+a[x+1] <= m)dfs(x+1,cur+a[x+1]);dfs(x+1,cur);
}

其实这些代码都是等价的,画一个搜索树就看出来了。但是刚开始写也会写崩、、反正还是没理清楚到底表示的是什么状态。 

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+10;ll a[maxn],b[maxn];
ll v[50];
ll w;
int pre[50];
int n;void init() {int i;pre[0]=1;for(i=1; i<=30; i++) pre[i]=2ll*pre[i-1];//2的n次方 
}int main() {ll t,sum,ans;int i,j,k,l,r,m,p;init();scanf("%d%lld",&n,&w);for(i=1; i<=n; i++) scanf("%lld",&v[i]);ans=0;for(i=0; i<pre[n/2]; i++) {t=i,sum=0,j=n/2,k=1;while(j>0) {if(t%2) sum+=v[k];t/=2,j--,k++;}a[i]=sum;if(sum<=w) ans=max(ans,sum);}for(i=0; i<pre[n-n/2]; i++) {t=i,sum=0,j=n-n/2,k=n/2+1;while(j>0) {if(t%2) sum+=v[k];t/=2,j--,k++;}b[i]=sum;if(sum<=w) ans=max(ans,sum);}sort(b,b+pre[n-n/2]);/*printf("*%d %d*\n",pre[n/2],pre[n-n/2]);for(i=0;i<pre[n/2];i++) printf("%lld ",a[i]);printf("\n");for(i=0;i<pre[n-n/2];i++) printf("%lld ",b[i]);printf("\n");*/for(i=0; i<pre[n/2]; i++) {if(a[i]<w) {l=0,r=pre[n-n/2]-1,p=-1;m=(l+r+1)/2;while(l<r) {m=(l+r+1)/2;if(a[i]+b[m]<=w) l=m;else r=m-1;}
//			printf("l = %d\n",l);if(l!=0) ans=max(ans,a[i]+b[l]);}}
//		for(i=0; i<pre[n/2]; i++) {
//		if(a[i]<w) {
//			l=0,r=pre[n-n/2]-1,p=-1;
//			while(l<=r) {
//				m=(l+r)/2;
//				if(a[i]+b[m]<=w) l=m+1,p=m;
//				else r=m-1;
//			}
//			printf("p = %d\n",p);
//			if(p!=-1) ans=max(ans,a[i]+b[p]);
//		}
//	}printf("%lld\n",ans);return 0;
}

骚操作:

#include<bits/stdc++.h>
#define Mit map<int,bool>::iterator
using namespace std;
typedef long long ll;
map<int,bool> M;
vector<int> V;
int main()
{int n,w;scanf("%d%d",&n,&w);int x;for(int i=0;i<n;i++){scanf("%d",&x);V.clear();if(x<=w){V.push_back(x);for(Mit it=M.begin();it!=M.end()&&it->first<=w-x;it++){V.push_back(it->first+x);}for(int j=0;j<V.size();j++){M[V[j]]=true;}}}ll ans=0;Mit it=M.upper_bound(w);if(it!=M.begin()){it--;ans=it->first;}printf("%d\n",ans);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441531.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

windowsthinpc虚拟内存_windows thin pc如何开启windows功能

没有“\DS\”目录 (子目录)没有“\Packages\”目录(子目录)没有“\FeaturePack\”目录(子目录)没有“\LangPacks\"目录(子目录)未找到“winemb-premiumcodecs-dolby-ac3-audioencoder.cab”未找到“WinEmb-PremiumCodecs-MPEG2andDolbyDecoder.cab”未找到“WinEmb-Premiu…

【牛客 - 185F】 假的数学游戏(斯特林公式,大数Java打表)

题干&#xff1a; 输入描述: 第一行&#xff1a;一个整数X 输出描述: 第一行&#xff1a;一个整数N 示例1 输入 复制 7 输出 复制 10 备注: 每个测试点所对应的X满足&#xff1a;第i个测试点输入的值为第i-1个测试点输入的值乘以10再加上7。特别的&#xff0c;第一个…

js怎么调用wasm_对于WebAssembly编译出来的.wasm文件js如何调用

WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍主要是让大家知道在js里面如何调用执行它&#xff0c;我之前看WebAssemblyAPI时候反正是看得一脸懵逼也是为了大家能更快的入手这个比较新的技术吧这边写的一个dom是官方推荐的c/c编译的c代码1 int add (int …

*【HDU - 6333】Problem B. Harvest of Apples (莫队,逆元,组合数学)(这样预处理正确吗?)

题干&#xff1a; There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm apples. Input The first line of the input contains an integer TT (1≤T≤105)(1≤T≤105) denoting the number of test cases. Each test c…

ruoyi 多模块部署_大数据时代,独立部署的商城系统具有哪些优势?

独立部署是把一个可部署软件包安装到一个指定IT环境上并让其按预定流程提供服务的过程。现如今&#xff0c;越来越多的商家开始搭建网上商城系统&#xff0c;从而为自己积攒多年的客户群体提供更为便捷的购物模式&#xff0c;让购物变得更加智能化。独立部署的商城系统具有哪些…

【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队)

题干&#xff1a; Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai1, ..., aj. Inpu…

freerdp 解压安装_Ubuntu下编译安装运作FreeRdp连接Windows

Ubuntu下编译安装运行FreeRdp连接Windows.linux下编译源码进行USB重定向到远程桌面的测试&#xff0c;经过参数的正确配置&#xff0c;可以正常的重定向。具体的操作步骤如下:⑴在虚拟机上安装git,安装命令如&#xff1a;sudo apt-get install git。⑵从GitHub下载FreeRdp源码&…

【HDU - 3328】Flipper (栈模拟)

题干&#xff1a; Problem Description Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (C…

trunk口不通防火墙_交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式

问题&#xff1a;交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式&#xff0c;两端正常配置后&#xff0c;端口状态显示错误&#xff0c;Eth-trunk端口无法up 。问题描述&#xff1a;交换机侧 GE0/0/5和GE 0/0/6 组成Eth-trunk3 通过LACP-static与防火墙对接&…

【HDU - 1850】Being a Good Boy in Spring Festival (尼姆博弈,nim,异或前缀和)

题干&#xff1a; 一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 如果愿意 你还可以和爸妈说 咱们玩个小游戏吧 ACM课上学的呢…

comsol临时文件夹中有不支持的字符_Editor 常见问题

Editor的数据存在哪里&#xff1f;Editor所处理的数据&#xff0c;就是你自己本地电脑的文件夹、图片、文本文档。除了配置信息、一些必要的临时缓存之外&#xff0c;Editor没有特殊格式的数据。所以&#xff0c;数据都在电脑里&#xff1b;比如说需要备份的话&#xff0c;只要…

【51nod - 1098】 最小方差(基础数学,公式化简,前缀和,积的前缀和)

题干&#xff1a; 若x1,x2,x3……xn的平均数为k。 则方差s^2 1/n * [(x1-k)^2(x2-k)^2…….(xn-k)^2] 。 方差即偏离平方的均值&#xff0c;称为标准差或均方差&#xff0c;方差描述波动程度。 给出M个数&#xff0c;从中找出N个数&#xff0c;使这N个数方差最小。 Input …

【牛客 - 181B】送分题(另类求解a+b,二分)

题干&#xff1a; 对于一套题来说&#xff0c;没有一道送分题&#xff0c;就很不符合常理&#xff0c;但是我又懒得写送分题&#xff0c;所以你可以直接复制以下代码&#xff0c;即可ac本题. #include<cstdio>#include<iostream> using namespace std; int a,…

牧马人机械鼠标g3_性价比好的有线鼠标都有哪些?2020年12款热选游戏鼠标推荐...

决定一款鼠标好坏因素众多&#xff0c;我们常常发现一款鼠标&#xff0c;有些用户觉得非常好&#xff0c;也有些用户觉得不好&#xff0c;是因为大多数用户对于鼠标的尺寸大小、轻重、实际的手感等方面都会有所不同&#xff0c;这都很正常&#xff0c;所以适合自己才是最好的。…

emc re 整改 超标_老刘工程师睡前故事5-EMC 辐射发射超标怎么办?

老刘工程师睡前故事-EMC 辐射发射超标怎么办&#xff1f;今天的工程师睡前故事讲讲汽车EMC测试中辐射发射超标了怎么办?首先来聊聊辐射发射的基本知识辐射发射英文简称RE&#xff0c;检测的是产品对外的辐射干扰。国际标准参考CISPR25&#xff0c;国标参考GBT18655。这个标准分…

全国计算机等级考试职称,全国职称计算机考试与全国计算机等级考试有什么不同?...

落羽杉属共有三个树种&#xff1a;落羽杉(Taxodium.distichum)、池杉(T.ascendens)和墨西哥落羽杉(T.mucronatum)。苏杉一号(T. distichum ‘Sushang’)是从落羽杉不同地理种源、家系中选育出的优良种源。中山杉302是从落羽杉和墨西哥落羽杉杂种中选育出的优良无性系(另外&…

【PAT - 甲级 - 1018】Public Bike Management (带权最短路,多条最短路中加条件,DFS)

题干&#xff1a; 链接&#xff1a;https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f 来源&#xff1a;牛客网 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. On…

【牛客 - 181C】序列(前缀和,二分,可用set维护)(有坑)

题干&#xff1a; 小a有n个数&#xff0c;他想把他们划分为连续的权值相等的k段&#xff0c;但他不知道这是否可行。 每个数都必须被划分 这个问题对他来说太难了&#xff0c;于是他把这个问题丢给了你。 输入描述: 第一行为两个整数n,q&#xff0c;分别表示序列长度和询问…

【CodeForces - 122B 】Lucky Substring (字符串,水题)

题干&#xff1a; Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya was …

*【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结)

题干&#xff1a; Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number…