poj 1064(注意二分搜索变形中l,r,mid的取值和结果取mid还是l 抑或r )

#include<iostream>
#include<cmath>
using namespace std;
int n,k;
int data[10005];
bool check(int len){
    int sum = 0;
    for(int i=0;i<n;i++){
        sum += data[i]/len;
        if(sum>=k)return true; 
    }
    return false;
}
int main(){
    scanf("%d%d",&n,&k);
    double tmp;
    for(int i=0;i<n;i++){
        scanf("%lf",&tmp);
        data[i] = tmp*100;
    }
    int l,r,mid;
    l = 0;r = 10000000;
    while(l<r){
        mid = (l+r+1)/2;
        if(check(mid))l = mid;
        else r = mid-1; 
    }
    printf("%.2f",r/100.0);
    return 0;
}

 

上一篇:小国王 1064


下一篇:1064 朋友数 (20 point(s))