网站后台改网页底色,网站建设文案策划,宁德网站开发,网站模版亮点为什么把二分和离散化放一起#xff1a;因为离散化其实是一种二分整数的过程。
二分
相信大家都接触过二分查找#xff08;折半查找#xff09;#xff0c;这就是二分的思想。
二分通过每次舍弃一半并不存在答案的区间#xff0c;进而快速锁定要求的答案#xff08;二…为什么把二分和离散化放一起因为离散化其实是一种二分整数的过程。
二分
相信大家都接触过二分查找折半查找这就是二分的思想。
二分通过每次舍弃一半并不存在答案的区间进而快速锁定要求的答案二分一定有解但解不一定就是答案后面会说
二分模板
bool check(int x) {/* ... */} // 检查x是否满足某种性质// 区间[l, r]被划分成[l, mid]和[mid 1, r]时使用
int bsearch_1(int l, int r)
{while (l r){int mid l r 1;if (check(mid)) r mid; // check()判断mid是否满足性质else l mid 1;}return l;
}
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用
int bsearch_2(int l, int r)
{while (l r){int mid l r 1 1;if (check(mid)) l mid;else r mid - 1;}return l;
}说一下版子二为什么要1因为涉及到mid - 11是为了防止数组越界的l r 所以r 0所以( r 1 1) 1因而r更新的时候一定大于等于0这也就防止了越界。
当然这只是针对于整数二分的边界问题浮点数二分就不用考虑这个多了直接除2就可以。
例题
1、AcWing 789. 数的范围 - AcWing
2、AcWing 790. 数的三次方根 - AcWing
题一直接套用两个模板二分出左右区间。判断-1的方法首次二分出来的区间的下标对应的数组元素并不等于给定要查找的那个数。 题二不要的左右边界设置成-n 和 n这样无法处理小数的情况因为他们的三次方根都会落在-n到n范围的外面但它也会有解。这也解释了为什么二分一定有解但是解不一定是答案解不对
离散化
先来看一下百科的离散化的定义
离散化把无限空间中有限的个体映射到有限的空间中去以此提高算法的时空效率。
通俗的说离散化是在不改变数据相对大小的条件下对数据进行相应的缩小。例如
原数据1,999,100000,15处理后1,3,4,2
原数据{100,200}{20,50000}{1,400}
处理后{3,4}{2,6}{1,5} 离散化就是把一些分布很稀疏的数重新按着他的序号排序比如我们现在有数据10^9、 1、 5000、 100000 这组数离散化之后的结果就是4、 1、 2、 3 可以看到结果其实就是他们的次序大小。
一般的我们先把这组数据排序然后在离散化这样得到的结果就是1、2、3、4、5、6.... n.一组连续的整数这样就可以存到数组里面然后随机访问。
当题目中给的数据范围很大比如是-10^9到10^9但是数据规模很小如n 10^5。这时候首当其中的就要考虑离散化。因为我们无法创建一个合适大小的数组所以基于数组随机访问的bucket等算法思想就无法使用但当我们离散化之后就可以用一个10^5的数组去存放这些数因为只有这些个数据有效。 在离散化的时候我们一般要考虑去重问题可以理解成在同一个位置上存放两次数据所以不需要给它重新分配下标。
然后说一下怎么去重 unique函数 他会把一段连续的数据内的相同元素删掉并返回指向最后一个不重复元素的下一个地址的迭代器。 unique参数两个维护范围的迭代器 这样我们就得到的了一个缩减版的数组和一个指向数组有效数据的下一个位置的指针如果我们用vector的话调用erase函数把剩余的无效数据的部分释放掉就得到了一个无重复数据的容器。 现在我们得到了一个无重复数据的递增的vector可以正式开始离散化了离散化也是二分求下标的过程。
离散化模板
int find(int x)
{int l 0, r alls.size() - 1;while(l r){int mid l r 1;if(alls[mid] x) r mid;else l mid 1;}return r 1;
}
解释一下参数x为想要离散化数组的其中一个数据返回值为离散化后的相对大小或者叫新下标这里是从1开始。 例题
这一题用得到知识点离散化、前缀和、二分。
区间和 文章转载自: http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.dwncg.cn.gov.cn.dwncg.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn