ACM 二分查詢 二分答案 模板
阿新 • • 發佈:2018-12-23
我自己的模板
int l=0,r=100,ans=0;
while(l<=r)
{
mid=(l+r)/2;
if (check(mid)) {l=mid+1;ans = mid; }else r=mid-1; //注意+1,-1否則死迴圈
}
男隊長的模板
int main(){ == key int m; while ( l <= r ) { m = ( l + r ) >> 1; if ( x[m] == key ) return m; else if ( key > x[m] ) l = m + 1; else r = m - 1; } >= key 中最小的 int ret; while ( l <= r ) { m = ( l + r ) >> 1; if ( x[m] >= key ) { ret = m; r = m - 1; } else l = m + 1; } <= key 中最大的 int ret; while ( l <= r ) { m = ( l + r ) >> 1; if ( x[m] <= key ) { ret = m; l = m + 1; } else r = m - 1; } }