当前位置: 首页 > news >正文

鄂尔多斯网站建设网站导航怎么用ulli做

鄂尔多斯网站建设,网站导航怎么用ulli做,网站备案组织机构代码,网站建设图书### 9.9 01背包问题#xff08;一维二维#xff09; 背包问题分类#xff1a;01背包#xff08;一种物品只有一个#xff09;#xff0c;完全背包#xff08;一种物品有无数个#xff09;#xff0c;多重背包#xff08;不同物品有不同数量#xff09; 46. 携带研究…### 9.9 01背包问题一维二维 背包问题分类01背包一种物品只有一个完全背包一种物品有无数个多重背包不同物品有不同数量 46. 携带研究材料第六期模拟笔试 题目描述 小明是一位科学家他需要参加一场重要的国际科学大会以展示自己的最新研究成果。他需要带一些研究材料但是他的行李箱空间有限。这些研究材料包括实验设备、文献资料和实验样本等等它们各自占据不同的空间并且具有不同的价值。  小明的行李空间为 N问小明应该如何抉择才能携带最大价值的研究材料每种研究材料只能选择一次并且只有选与不选两种选择不能进行切割。 输入描述 第一行包含两个正整数第一个整数 M 代表研究材料的种类第二个正整数 N代表小明的行李空间。 第二行包含 M 个正整数代表每种研究材料的所占空间。  第三行包含 M 个正整数代表每种研究材料的价值。 输出描述 输出一个整数代表小明能够携带的研究材料的最大价值。 暴力解法每个物品只有取和不取用回溯算法枚举就可以。时间复杂度n2 01背包问题 二维  https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html   https://www.bilibili.com/video/BV1cg411g7Y6   dp[i][j]:i 来表示物品、j表示背包容量 dp[i][j] 表示从下标为[0-i]的物品里任意取放进容量为j的背包价值总和最大是多少。 如dp[1][4] 表示什么意思呢? 任取物品0、物品1放入容量为4的背包中最大价值是dp[1][4]。 二维数组遍历顺序先遍历物品再遍历背包/先遍历背包再遍历物品都可以。因为需要的值就是正上方和左上角的值。 遇到的问题 非leetcode包装好的方法需要获取数据 写的时候也有一些细节需要注意 java import java.util.Scanner;      public class bag01TwoDimensional {       public static void main(String[] args) {           Scanner sc new Scanner(System.in);           int num sc.nextInt();           int bagCapacity sc.nextInt();              int[] materialSpace new int[num];           int[] materialValue new int[num];              for (int i 0; i num; i) {               materialSpace[i] sc.nextInt();           }           for (int i 0; i num; i) {               materialValue[i] sc.nextInt();           }              //dp初始化           int[][] dp new int[num][bagCapacity1];           //初始化第一行有简洁的写法即直接从materialSpace[0]开始遍历。如果它大于包的容量则不进入循环。           for (int i materialSpace[0]; i bagCapacity; i) {               dp[0][i] materialValue[0];           }           //初始化第一列其实不用写默认的初始值就是0              for (int i 1; i num; i) {               for (int j 0; j bagCapacity; j) {   //                //不要第i个物品   //                int noI dp[i-1][j];   //                //要第i个物品   //                int withI dp[i-1][j-materialSpace[i]] materialValue[i];                   if(materialSpace[i] j){                       dp[i][j] dp[i-1][j];                   }else{                       dp[i][j] Math.max(dp[i-1][j], dp[i-1][j-materialSpace[i]] materialValue[i]);                   }               }           }   //        for (int i 0; i num; i) {   //            for (int j 0; j bagCapacity; j) {   //                System.out.print(dp[i][j] dp[i][j]  );   //            }   //            System.out.println();   //        }           System.out.println(dp[num-1][bagCapacity]);       }   } 01背包问题 一维  https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-2.html   https://www.bilibili.com/video/BV1BU4y177kY   把矩阵压缩成一行把上一层拷贝到下一层。滚动数组。 1.确定dp数组dp table以及下标的含义 dp[j] 容量为j的背包能装进物品的最大价值。 2.确定递推公式  不放物品idp[j] 放物品idp[j-weight[i]]value[i] 3.dp数组如何初始化0/1确认很有讲究很重要 dp[0] 0; 4.确定遍历顺序 倒序遍历。如果正序遍历会导致前面的物品被反复加入 5.打印dp数组动态规划代码简短直接看打印出来的dp数组是不是和自己设想的一致 java public class bag02OneDimensional {       public static void main(String[] args) {   //        int materialNum 6;   //        int capacity 6;   //   //        int[] weight {2,2,3,1,5,2};   //        int[] value {2,3,1,5,4,3};              //get data           Scanner sc new Scanner(System.in);           int materialNum sc.nextInt();           int capacity sc.nextInt();              int[] weight new int[materialNum];           int[] value new int[materialNum];              for (int i 0; i materialNum; i) {               weight[i] sc.nextInt();           }              for (int i 0; i materialNum; i) {               value[i] sc.nextInt();           }              int[] dp new int[capacity1];//当背包容量为i时包内物品的最大价值为dp[i]              for (int i 0; i materialNum; i) {               for (int j capacity; j weight[i]; j--) {                   int withoutI dp[j];                   int withI dp[j-weight[i]] value[i];                   dp[j] Math.max(withoutI,withI);                   //System.out.println(dp[i][j] dp[j]);               }           }              System.out.println(dp[capacity]);          }   } ### 9.10 416. Partition Equal Subset Sum Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. https://leetcode.cn/problems/partition-equal-subset-sum/description/  416. 分割等和子集   本题是 01背包的应用类题目 https://programmercarl.com/0416.%E5%88%86%E5%89%B2%E7%AD%89%E5%92%8C%E5%AD%90%E9%9B%86.html     https://www.bilibili.com/video/BV1rt4y1N7jE 01背包里面的元素能不能刚好装满容量为数组元素和二分之一的背包。 物品的重量和价值相同 动态规划五部曲 1.确定dp数组dp table以及下标的含义 dp[j]:背包总容量为j放进物品后背的最大重量为dp[j]。 2.确定递推公式  3.dp数组如何初始化0/1确认很有讲究很重要 4.确定遍历顺序 后序遍历 5.打印dp数组动态规划代码简短直接看打印出来的dp数组是不是和自己设想的一致 自己做的时候错误点 没有剪枝。当总和为奇数时直接返回false即可。 java public class partitionEqualSubsetSum {       public boolean canPartition(int[] nums) {           int sum 0;           for (int i 0; i nums.length; i) {               sum   nums[i];           }           if(sum%2 1) return false;           int target sum/2;              int[] dp new int[target 1];           //先遍历物品后遍历背包           for (int i 0; i nums.length; i) {               for (int j target; j nums[i]; j--) {   //                int withI dp[j - nums[i]] nums[i];   //                int withoutI dp[j];                   dp[j] Math.max(dp[j - nums[i]] nums[i],dp[j]);               }           }           return dp[target] target;       }   }   class partitionEqualSubsetSumTest{       public static void main(String[] args) {           int[] nums {1,2,3,5};           partitionEqualSubsetSum example new partitionEqualSubsetSum();           System.out.println(example.canPartition(nums));          }   } ### 9.11 1049. Last Stone Weight II You are given an array of integers stones where stones[i] is the weight of the ith stone. We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights x and y with x y. The result of this smash is: If x y, both stones are destroyed, and If x ! y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x. At the end of the game, there is at most one stone left. Return the smallest possible weight of the left stone. If there are no stones left, return 0. https://leetcode.cn/problems/last-stone-weight-ii/description/ 1049. 最后一块石头的重量 II 本题就和 昨天的 416. 分割等和子集 很像了可以尝试先自己思考做一做。  https://www.bilibili.com/video/BV14M411C7oV  https://programmercarl.com/1049.%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8FII.html   把石头尽量分成重量相近的两堆比如总重量为23的石头堆分成11和12。 java public class lastStoneWeight2 {       public int lastStoneWeightII(int[] stones) {           int sum 0;           for (int i 0; i stones.length; i) {               sum stones[i];           }           int target sum/2;              int[] dp new int[target1];              for (int i 0; i stones.length; i) {               //如果此时背包剩余容量小于待加入石头的重量则没必要进入循环               //If the remaining capacity of the backpack at this time is less than the weight of the stones to be added, there is no need to enter the loop               for (int j target; j stones[i]; j--) {                   dp[j] Math.max(dp[j],dp[j-stones[i]] stones[i]);               }           }              //return Math.abs(dp[target]-(sum-dp[target]));           return sum - 2*dp[target];       }   }   class lastStoneWeight2Test{       public static void main(String[] args) {           lastStoneWeight2 example new lastStoneWeight2();           int[] nums {31,26,33,21,40};           System.out.println(example.lastStoneWeightII(nums));       }   } ### 9.12 494. Target Sum You are given an integer array nums and an integer target. You want to build an expression out of nums by adding one of the symbols and - before each integer in nums and then concatenate all the integers. For example, if nums [2, 1], you can add a before 2 and a - before 1 and concatenate them to build the expression 2-1. Return the number of different expressions that you can build, which evaluates to target. https://leetcode.cn/problems/target-sum/description/  494. 目标和  大家重点理解 递推公式dp[j] dp[j - nums[i]]这个公式后面的提问 我们还会用到。   https://www.bilibili.com/video/BV1o8411j73x https://programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html   分成两个子集加法在一个子集(left)减法在一个子集(right)。 sum left right     left sum - right target left - right   left target right 2left sum target left (sumtarget)/2 如果不能整除就是凑不成target直接返回0 有多少种情况可以装满背包就返回多少 nums [1,1,1,1,1], target 3 dp[j] : 装满容量为j的背包有dp[j] 种方法 背包里已有 物品1:  有dp[4] 种方法凑成dp[5]; 物品2:  有dp[3] 种方法凑成dp[5]; 递推公式 dp[j] dp[j-num[i]]; 初始化dp[0] 1; java public class targetSum {       public int findTargetSumWays(int[] nums, int target) {           int sum 0;           for (int i : nums) {               sum i;           }           //如果target的绝对值大于sum那么无解           if ((sum target) % 2 1 || Math.abs(target) sum) return 0;              int capacity (sum target) 1;              int[] dp new int[capacity 1];           dp[0] 1;           for (int i 0; i nums.length; i) {               for (int j capacity; j nums[i]; j--) {                   dp[j] dp[j-nums[i]];               }           }            return dp[capacity];       }   }      class targetSumTest {       public static void main(String[] args) {           targetSum example new targetSum();           int[] nums {1,1,1,1,1};           int target 3;           System.out.println(example.findTargetSumWays(nums, target));       }   } ### 9.13 474. Ones and Zeroes You are given an array of binary strings strs and two integers m and n. Return the size of the largest subset of strs such that there are at most m 0s and n 1s in the subset. A set x is a subset of a set y if all elements of x are also elements of y. https://leetcode.cn/problems/ones-and-zeroes/description/ 474.一和零   通过这道题目大家先粗略了解 01背包完全背包多重背包的区别不过不用细扣因为后面 对于 完全背包多重背包 还有单独讲解。 https://www.bilibili.com/video/BV1rW4y1x7ZQ  https://programmercarl.com/0474.%E4%B8%80%E5%92%8C%E9%9B%B6.html   装满容器最多多少个物品 m个0 n个1 凑成子集 背包最多有m个0n个1。装满这个背包最多有多少个物品 dp[i][j] 装满i个0j个1的背包最多有dp[i][j]个物品 待加入物品有x个0y个1 dp[i][j] Math.max((dp[i-x][j-y] 1),dp[i][j]) 初始化 dp[0][0] 0; 非零坐标也初始化为0。防止递推公式求出的值被初始值覆盖。 java public class onesAndZero {       public int findMaxForm(String[] strs, int m, int n) {           int[][] dp new int[m 1][n 1];              //iterate items first           for (String s : strs) {               int x 0;               int y 0;               //to get how many 0 and i in this element               char[] chars s.toCharArray();               for (char c : chars) {                   if (c 0) {                       x;                   } else {                       y;                   }               }                  //and then iterate the bag               for (int i m; i x; i--) {                   for (int j n; j y; j--) {                       dp[i][j] Math.max((dp[i - x][j - y] 1), dp[i][j]);                   }               }           }           return dp[m][n];       }   }      class onesAndZeroTest {       public static void main(String[] args) {           onesAndZero example new onesAndZero();           String[] strs {10,0,1};           int m 1;           int n 1;              System.out.println(example.findMaxForm(strs,m,n));          }   }
文章转载自:
http://www.morning.fpyll.cn.gov.cn.fpyll.cn
http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn
http://www.morning.nrchx.cn.gov.cn.nrchx.cn
http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn
http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn
http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn
http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com
http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn
http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn
http://www.morning.nxfuke.com.gov.cn.nxfuke.com
http://www.morning.srsln.cn.gov.cn.srsln.cn
http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn
http://www.morning.ysckr.cn.gov.cn.ysckr.cn
http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn
http://www.morning.ylklr.cn.gov.cn.ylklr.cn
http://www.morning.bcngs.cn.gov.cn.bcngs.cn
http://www.morning.kybjr.cn.gov.cn.kybjr.cn
http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn
http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn
http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn
http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn
http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn
http://www.morning.rywr.cn.gov.cn.rywr.cn
http://www.morning.qhln.cn.gov.cn.qhln.cn
http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn
http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn
http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn
http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn
http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn
http://www.morning.bkryb.cn.gov.cn.bkryb.cn
http://www.morning.czrcf.cn.gov.cn.czrcf.cn
http://www.morning.rjbb.cn.gov.cn.rjbb.cn
http://www.morning.sqlh.cn.gov.cn.sqlh.cn
http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn
http://www.morning.prfrb.cn.gov.cn.prfrb.cn
http://www.morning.wgrm.cn.gov.cn.wgrm.cn
http://www.morning.yppln.cn.gov.cn.yppln.cn
http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn
http://www.morning.wqgr.cn.gov.cn.wqgr.cn
http://www.morning.cprbp.cn.gov.cn.cprbp.cn
http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn
http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn
http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn
http://www.morning.krlsz.cn.gov.cn.krlsz.cn
http://www.morning.whothehellami.com.gov.cn.whothehellami.com
http://www.morning.snygg.cn.gov.cn.snygg.cn
http://www.morning.mnslh.cn.gov.cn.mnslh.cn
http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn
http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn
http://www.morning.yzktr.cn.gov.cn.yzktr.cn
http://www.morning.trqzk.cn.gov.cn.trqzk.cn
http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn
http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn
http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn
http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn
http://www.morning.duqianw.com.gov.cn.duqianw.com
http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn
http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn
http://www.morning.mghgl.cn.gov.cn.mghgl.cn
http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn
http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn
http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn
http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn
http://www.morning.zyrp.cn.gov.cn.zyrp.cn
http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn
http://www.morning.rbzht.cn.gov.cn.rbzht.cn
http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn
http://www.morning.njqpg.cn.gov.cn.njqpg.cn
http://www.morning.rysmn.cn.gov.cn.rysmn.cn
http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn
http://www.morning.srxhd.cn.gov.cn.srxhd.cn
http://www.morning.fykqh.cn.gov.cn.fykqh.cn
http://www.morning.yuminfo.com.gov.cn.yuminfo.com
http://www.morning.dqwykj.com.gov.cn.dqwykj.com
http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn
http://www.morning.knlyl.cn.gov.cn.knlyl.cn
http://www.morning.hncrc.cn.gov.cn.hncrc.cn
http://www.morning.knzdt.cn.gov.cn.knzdt.cn
http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn
http://www.morning.zwtp.cn.gov.cn.zwtp.cn
http://www.tj-hxxt.cn/news/273868.html

相关文章:

  • wordpress调用菜单的代码中国临沂网站优化
  • 哪里可以做网站教程网站开发一定要用框架吗
  • 哪个视频网站做视频赚钱的高端网站定制策划
  • 外贸建站效果神马搜索seo优化排名
  • ps个人网站怎么做怎样给响应式网站提速
  • 广州建站软件深圳网站建设服务哪一个便宜
  • 个人网站备案号被注销页面设计怎么设计
  • 怎样网站不用备案xxx网站建设与优化推广
  • 代做标书网站巩义做网站的
  • 网站模板怎么制作如何制作网站详细教程
  • 海外营销网站传世网站建设
  • 网站开发的配置过程wordpress做服务器配置
  • 阿里云主机 多个网站在线做编程题的网站
  • 浙江省两学一做网站北京 公司网站 备案中 开通访问
  • 网站平台维护设计公司名字怎么取
  • 做网站软件定制开发平台推广员是干嘛的
  • 豫icp郑州网站建设国外seo查询
  • 网站建设公司的性质泰州网站建设找思创
  • 浙江省建设厅网站地址中国建设工程
  • 公司网站建站要多少钱怎么建设网站网站
  • php网站设置如何使用百度小说搜索热度排行榜
  • dw 8做的网站怎么上传夸克资源搜索引擎
  • 重庆网站开发公司客户管理软件公司
  • 新站如何快速收录天津百度推广公司电话
  • 钦州建设银行社招聘网站十堰微网站建设
  • 邵阳做网站的公司建设银行网站打不开 显示停止工作
  • 建设信用卡积分商城网站next wordpress
  • 泉州建设局网站可作外链的网站
  • 网站建设没有业务怎么办长春微信网站建设
  • 网站建设淄博佳铉网络宝安网站建设公司968