二分搜索(判断某一个解可不可以) ------》POJ 1064

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40984919/article/details/81302798

题目链接:http://poj.org/problem?id=1064

题意:让你找出切割成K条相同的绳子之后,最长的有多长。

这里我们可以用到二分搜索(将每一种情况枚举出来,最后找到一个最小值)

#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<math.h>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define close ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N=1000000+50;
const int INF=0x3f3f3f3f;
ll mod = 1e9+7;

int N,K;
double L[MAX_N];

bool C(double x){
	int num = 0;
	for(int i = 0; i < N; i++){
		num += (int)(L[i] / x);
	}
	return num >= K;
}

int main(){
	cin>>N>>K;
	for(int i = 0; i < N; i++){
		scanf("%lf",&L[i]);
	}
	double lb = 0, ub = 100000.0;
	
	//重复循环  找最优解
	for(int i = 0; i < 100; i++){
		double mid = (lb + ub) / 2;
		if(C(mid)) lb = mid;
		else ub = mid;
	} 
	printf("%.2f\n",floor(ub*100) / 100);
 	return 0;
}

/*
                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############
*/

猜你喜欢

转载自blog.csdn.net/qq_40984919/article/details/81302798