首页 > 其他分享 >买瓜-dfs

买瓜-dfs

时间:2024-04-10 19:31:09浏览次数:17  
标签:cnt return int sum 买瓜 dfs 当前

6.买瓜 - 蓝桥云课 (lanqiao.cn)

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,m,a[35],cnt=40,sum[35];
//x是当前编号,w是当前总重量,c是当前劈了多少个瓜
void dfs(int x,int w,int c){
	if(w==m){//如果当前重量符合要求了,就更新一下是不是最小值,然后退出
		cnt=min(cnt,c);
		return;
	}
	if(x>=n) return;//当前的所有瓜都遍历一遍了,就退出
	if(w>m) return;//当前的重量超了,就退出
	if(c>=cnt) return;//当前劈的瓜的数量超过目前的答案了,再继续下去也不是最优解了,就退出
	if(w+sum[n-1]-sum[x-1]<m) return;//当前剩下的没遍历的瓜,就算都算上也达不到所要求的重量就退出
	//注意,下面三个dfs的顺序,重量最多的(因为最有可能是最优解)最先进行dfs
	dfs(x+1,w+a[x],c);//当前的瓜不劈开
	dfs(x+1,w+a[x]/2,c+1);//当前的瓜劈开
	dfs(x+1,w,c);//当前的瓜不买
}
bool cmp(int a,int b){
	return a>b;
}
void solve(){
	cin>>n>>m;
	//这里和下面乘以2,是因为劈一刀,有可能是小数,有小数点0.5,乘以2把小数点去掉
	//方便上面dfs的w==m判断相等,float和double我怕精度原因不相等,乘以2就可以整数int判断相等了
	m=m*2;
	for(int i=0;i<n;i++){
		cin>>a[i];
		a[i]*=2;
		//下面是前缀和,存到当前i的重量的累加值
		if(i>0)
			sum[i]=sum[i-1]+a[i];
		else
			sum[i]=a[i];
	}
	sort(a,a+n,cmp);//排序一下,从大到小排序,dfs的时候就能从大到小,这样求最优解更快
	dfs(0,0,0);
	if(cnt==40){//如果没找到答案,即cnt还是初始值
		cout<<-1;
	}
	else cout<<cnt;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	solve();
	return 0;
}

标签:cnt,return,int,sum,买瓜,dfs,当前
From: https://blog.csdn.net/weixin_73214301/article/details/137560994

相关文章

  • Acwing2024蓝桥杯DFS
    模板:AcWing826.单链表利用数组创建单链表:#include<iostream>usingnamespacestd;constintN=100005;inthead,value[N],nextt[N],id;voidInit(){head=-1,id=0;}voidHead_Insert_x(intx){value[id]=x;nextt[id]=head;head=id++;}voidD......
  • HDFS报错:Couldn‘t preview the file.
    packagecom.qm.hdfs;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.Path;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importjava.io.IOException;importjava.n......
  • Java实现Fast DFS、服务器、OSS上传
    支持FastDFS、服务器、OSS等上传方式介绍在实际的业务中,可以根据客户的需求设置不同的文件上传需求,支持普通服务器上传+分布式上传(FastDFS)+云服务上传OSS(OSS)软件架构为了方便演示使用,本项目使用的是前后端不分离的架构前端:Jquery.uploadFile后端:SpringBoot前期准备:F......
  • Acwing 681. 疏散人群(dfs)(记录根节点下有几个子节点)
    输入样例:62132435261输出样例:4#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<LL,LL>PII;constLLN=100200,M=2020;constLLmod=998244353;vector<LL>g[N];LLsum[N];LLdfs(LLidx,LLfa){LL......
  • 代码随想录 | 图论 797. 所有可能的路径(dfs) ,200. 岛屿数量 (dfs)200. 岛屿数量 (bfs)
    797.所有可能的路径https://leetcode.cn/problems/all-paths-from-source-to-target/description/List<List<Integer>>res;List<Integer>path;publicList<List<Integer>>allPathsSourceTarget(int[][]graph){res=newA......
  • 4. 飞机降落-dfs
    4.飞机降落-蓝桥云课(lanqiao.cn)230100101010100220301020101020201020#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;structplane{intt,d,l;}p[12];intn;//pan数组用来判断当前飞机降落了么......
  • 蓝桥杯,省赛,dfs专题,地宫取宝,小朋友崇拜圈,飞机降落
    #include<bits/stdc++.h>usingnamespacestd;intn,m,k;inta[55][55];//输入所给数组值所分配的内存空间intdp[55][55][15][15];//开创记忆化的存储空间//因为只进行向下走和向右走,所有写成这个样子,不明白的可以在了解以下笛卡尔积,向下是x轴,向右是y轴(一般情况下)int......
  • dask读取hdfs文件时报错connect hdfs error
    问题详情:/arrow/cpp/src/arrow/filesystem/hdfs.cc:51:Failedtodisconnecthdfsclient:IOError:HDFShdfsFS::Disconnectfailed,errno:9(Badfiledescriptor)Traceback(mostrecentcalllast):File"/home/tdops/fucheng.pan/ray-code/read.py",line......
  • windos上安装hadoop并将文件上传至HDFS的操作
    参考1参考21.下载并安装hadoop下载解压hadoop:https://archive.apache.org/dist/hadoop/common/hadoop-2.7.1/百度网盘:安装包和配置文件链接:(https://pan.baidu.com/s/1SyORDDF5hxmm5-dZPuHNhA?pwd=1234)注意:我使用的是2.7.1版本,官网的Hadoop不支持Windows系统,需要修改......
  • 图的遍历-DFS
    1.DFS遍历DFS算法的思想:对一个无向连通图,在访问图中某一起始顶点v后,由v出发,访问它的某一邻接顶点w1;再从w1出发,访问与w1邻接但还没有访问过的顶点w2;然后再从w2出发,进行类似的访问;…;如此进行下去,直至到达所有邻接顶点都被访问过的顶点u为止;接着,回退一步,回退到前一......