題目的意思:告訴你幾個宿舍,然後每個宿舍有多少個房間,每個房間的編號是一次從第乙個加起來的,現在給你一些信封,信封上只有房間號,現在讓我們判斷這個房間是屬於哪個宿舍,在這個宿舍裡他是第幾個房間。
第一種方法手寫二分:
1 #include 2 #include3 #include 4 #include 5 #include 6 #include
7 #include 8910
using
namespace
std;
1112 typedef long
long
ll;13
14 ll a[2000005
];15 ll pre[2000005
];16
17ll binarysearch(ll a,ll n,ll key)
1828
return
left;29}
3031
intmain()
3242
ll temp;
43for (ll i=1;i<=m;i++)
4449
return0;
50 }
第二種方法:利用c++的函式
1 #include 2 #include3 #include 4 #include 5 #include 6 #include
7 #include 8910
using
namespace
std;
1112 typedef long
long
ll;13
14 ll a[2000005
];15 ll pre[2000005
];16
1718
intmain()
1929
ll temp;
30for (ll i=1;i<=m;i++)
3136
return0;
37 }
字首和 二分
powered by ab in 局外人 拿洛谷的乙個例子記一下字首和。資料超過了1e5,故o n 2 的演算法又行不通,所以換二分 include typedef long long ll using namespace std ll n,m,l,r ll a 1000001 sum 100000...
二分與字首和
時間複雜度o log n 資料區間 l mid r 實現 while l給定乙個按照公升序排列的長度為 n 的整數陣列,以及 q 個查詢。對於每個查詢,返回乙個元素 k 的起始位置和終止位置 位置從 0 開始計數 如果陣列中不存在該元素,則返回 1 1。輸入格式 第一行包含整數 n 和 q,表示陣列...
二分和字首和(藍橋)
二分 1.數的範圍 題意 給定乙個按照公升序排列的長度為n的整數陣列,以及 q 個查詢。1 n 100010,q 10000.對於每個查詢,返回乙個元素k的起始位置和終止位置 位置從0開始計數 如果陣列中不存在該元素,則返回 1 1 思路 對於已經排好序的,且要求查詢陣列中某乙個數的位置,屬於搜尋類...