网站建设资金管理办法,企业网站栏目结构,如何做全网营销推广,网站欣赏 公司网站案例文章目录算法模板整数二分算法模板浮点数二分算法模板模板题数的范围原题链接题目题解数的三次方根原题链接题目题解算法模板
整数二分算法模板
bool check(int x) {/* ... */} // 检查x是否满足某种性质// 区间[l, r]被划分成[l, mid]和[mid 1, r]时使用#xff1a;
int b…
文章目录算法模板整数二分算法模板浮点数二分算法模板模板题数的范围原题链接题目题解数的三次方根原题链接题目题解算法模板
整数二分算法模板
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;
}浮点数二分算法模板
bool check(double x) {/* ... */} // 检查x是否满足某种性质double bsearch_3(double l, double r)
{const double eps 1e-6; // eps 表示精度取决于题目对精度的要求一般比题目要求精度取小两个次方比较保险如要求1e-6则取1e-8while (r - l eps){double mid (l r) / 2;if (check(mid)) r mid;else l mid;}return l;
}
模板题
数的范围
原题链接
https://www.acwing.com/problem/content/791/
题目
789 . 数的范围 给定一个按照升序排列的长度为 n 的整数数组以及 q 个查询。
对于每个查询返回一个元素 k 的起始位置和终止位置位置从 0 开始计数。
如果数组中不存在该元素则返回 -1 -1。
输入格式 第一行包含整数 n 和 q表示数组长度和询问个数。
第二行包含 n 个整数均在 1∼10000 范围内表示完整数组。
接下来 q 行每行包含一个整数 k表示一个询问元素。
输出格式 共 q 行每行包含两个整数表示所求元素的起始位置和终止位置。
如果数组中不存在该元素则返回 -1 -1。
数据范围 1≤n≤100000 1≤q≤10000 1≤k≤10000 输入样例
6 3
1 2 2 3 3 4
3
4
5输出样例
3 4
5 5
-1 -1题解
#include iostream
using namespace std;const int N 1e6 10;
int n,m;
int q[N];int main(){scanf(%d%d,n,m);for(int i0;in;i) scanf(%d,q[i]);while(m--){int x; cinx;int l 0,r n-1;while(l r){int mid lr1;if(q[mid]x) r mid;else l mid 1;}if(q[l] ! x) cout-1 -1endl;else{coutl ;int l 0,r n-1;while(lr) {int mid lr1 1;if(q[mid]x) l mid;else r mid - 1;}coutlendl;}}return 0;
}数的三次方根
原题链接
https://www.acwing.com/problem/content/792/
题目
790 . 数的三次方根 给定一个浮点数 n求它的三次方根。
输入格式 共一行包含一个浮点数 n。
输出格式 共一行包含一个浮点数表示问题的解。
注意结果保留 6 位小数。
数据范围 −10000≤n≤10000 输入样例
1000.00输出样例
10.000000题解
#include iostream
#include math.h
using namespace std;
int main(){double n;cin n;double l -10000, r 10000;while(r-l 1e-8){ //一般精度比要求精度再多两个次方比较保险double mid (lr) / 2 ;if(pow(mid,3)n) r mid;else l mid;}printf(%.6lf,l);return 0;
}
文章转载自: http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn