Luogu P1577 Cut the rope

Luogu P1577 Cut the rope

Idea: Divide the length of each section of the rope, and then calculate how many sections there are. If there are more, move the left pointer (which can be larger), and vice versa. However, this question has accuracy problems, so the result is +0.0001 accuracy.
Code

#include<bits/stdc++.h>
using namespace std;
double a[1000001],l,r;
int i,n,m;
bool check(double x){
    
    
	long long s=0;
	for(i=1;i<=n;i++)
		s+=int(a[i]/x);
	if(s>=m)
		return true;
	return false;
}
int main(){
    
    
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+1+n);
	l=0;
	r=a[n];
	while(r-l>1e-10){
    
    
		double mid=(l+r)/2;
		if(check(mid))
			l=mid;
		else
			r=mid;
	}
	if(check(l))
		printf("%.2f",int(l*100 + 0.0001)/100.0);
	else
		printf("%.2f",int(r*100 + 0.0001)/100.0);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52536621/article/details/113921584