跳石头

来源:互联网 发布:手机蒙古软件大全 编辑:程序博客网 时间:2024/04/26 20:02
跳石头 - cxb6lkb - 石门实验中学卢凯宾的博客
 初看题目时并没有什么头绪,只想到了θ(n2)的暴力。后来老师提醒我们,答案是“最短跳跃距离”是可以枚举的,而且具有单调性,完全可以二分答案,时间复杂度只要θ(nlog2n)
具体的做法是:每次尝试一个最短距离值,并判定这个距离值有多少相邻的岩石能满足,如果是一种可行的方案就加大距离,否则缩小距离。但要注意,在计算时要考虑最后一块不可移动的岩石。
# include <cstdio>const int SIZE = 50005 ;int L , N , M , D[SIZE] ;bool check_ans ( int ) ;int binary_search () ;int main () {freopen ( "stone.in" , "r" , stdin ) ;freopen ( "stone.out" , "w" , stdout ) ;scanf ( "%d%d%d" , &L , &N , &M ) ;for ( int i = 1 ; i != N + 1 ; ++i ) {scanf ( "%d" , &D[i] ) ;}D[++N] = L++ ;int ans = binary_search () ;printf ( "%d\n" , ans ) ;return 0 ;}bool check_ans ( int dis ) {int last_pos = 0 , sum = 0 ;for ( int i = 1 ; i != N + 1 ; ++i )if ( D[i] - last_pos < dis ) {++ sum ;}else {last_pos = D[i] ;}return sum <= M ;}int binary_search () {int left_p = 1 , right_p = L ;while ( left_p + 1 < right_p ) {int mid_p = ( left_p + right_p ) >> 1 ;if ( check_ans ( mid_p ) ) {left_p = mid_p ;}else {right_p = mid_p ;}}return left_p ;}
0 0
原创粉丝点击