Find and cut trees in half

Two points

Find by half

Code template

while(l<=r)
{
    
    
	int mid=(l+r)/2;
	if(check(mid))
	{
    
    
		ans=mid;
		l=mid+1;
	}
	else
	{
    
    
		r=mid-1;
	}
}

Cut trees

https://www.luogu.com.cn/problem/P1873

Title description

Lumberjack Mirko needs to cut down M meters of wood. This is an easy job for Mirko, because he has a beautiful new logging machine that can chop down forests like wildfire. However, Mirko was only allowed to cut down single-row trees.

The working process of Mirko's timber harvester is as follows: Mirko sets a height parameter H (meters), and the timber harvester raises a huge saw blade to height H, and saw off all the parts of the tree higher than H (of course, the trees are not high The part above H meters remains unchanged). Mirko went to the sawn part of the tree.

For example, if the height of a row of trees is 20, 15, 10 and 17, and Mirko raises the saw blade to a height of 15 meters, the remaining height of the tree after cutting will be 15, 15, 10 and 15, and Mirko will Get 5 meters from the first tree and 2 meters from the fourth tree, for a total of 7 meters of wood.

Mirko is very concerned about ecological protection, so he will not cut down too much wood. This is why he set the feller blade as high as possible. Help Mirko find the maximum integer height H of the timber saw blade so that he can get at least M meters of wood. In other words, if he raises another meter by 1 meter, he will not get M meters of wood.

Input format

Line 1: 2 integers N and M, N represents the number of trees (1<=N<=1000000), M represents the total length of wood needed (1<=M<=2000000000)

Line 2: N integers represent the height of each tree, and the value does not exceed 1000000000. The sum of all wood lengths is greater than M, so there must be a solution.

Output format

Line 1: 1 integer, representing the highest height of the tree cut.

Sample input and output

Input #1
5 20
4 42 40 26 46
Output #1
36

In this question, the check content of check is whether the total height of the tree obtained meets the question

Code

#include <iostream>
#include <string>
using namespace std;

int n,m;
int ans=0;
int l=0,r=0;
int tree[1000005]={
    
    0};//注意数据范围

bool check(int x)
{
    
    
	long long s=0;//注意数据范围
	for(int i=1;i<=n;i++)
	{
    
    
		if(tree[i]>x)//判断第i棵的高度是否比锯片的高度高
		{
    
    
			s+=tree[i]-x;//将树砍下的高度累加
		}
	}
	return s>=m;//将高度和与需求高度m比较
}

int main()
{
    
    
  
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>tree[i];
		r=max(r,tree[i]);//存放最大值!
	}
  	
  	while(l<=r)//简单的对半查找
  	{
    
    
  		int mid=(l+r)/2;
  		if(check(mid))
  		{
    
    ;
  			l=;
		}
		else
		{
    
    
			r=;
		}
	}
  	
  	cout<<ans;//完美输出!
  	
 	return 0;
}

Solve one question, the next question:

timber processing

https://www.luogu.com.cn/problem/P2440

Topic background

To protect the environment

Title description

The lumber factory has some logs, and now I want to cut these wood into small pieces of the same length (the wood may have surplus), and the number of small pieces that needs to be obtained is given. Of course, we hope that the length of the small piece of wood is as long as possible. Your task is to calculate the maximum length of the small piece of wood that can be obtained. The unit of wood length is cm. The length of the logs are all positive integers, and we require that the length of the small pieces of wood cut is also a positive integer.

For example, there are two logs with lengths of 11 and 21, which are required to be cut into 6 equal lengths. Obviously, the longest length of the small wood that can be cut is 5.

Input format

The first line is two positive integers N and K (1 ≤ N ≤ 100000, 1 ≤ K ≤ 100000000), N is the number of logs, and K is the number of small segments that need to be obtained.

In the next N rows, each row has a positive integer between 1 and 100000000, indicating the length of a log.

Output format

The maximum length of the small segment that can be cut. If even the 1cm long section cannot be cut out, output "0".

Sample input and output

Input #1
3 7
232
124
456
Output #1
114

In this question, check is used to judge whether the number of segments to be cut meets the requirements of the question

Code

#include <iostream>
#include <string>
using namespace std;

int n,k;//N是原木的数目,K是需要得到的小段的数目
int treelen[100005]={
    
    0};
int l=0,r=0;
int ans=0;

bool check(int x)
{
    
    
	int s=0;
	for(int i=1;i<=n;i++)
	{
    
    
		if(x<=treelen[i]&&x!=0)//若能切割且切割长度>=1
		{
    
    
			s+=(treelen[i]/x);//段数
		}
		else if(x==0)//无法切割(长度<1)
		{
    
    
			return s=0;//无法切割即段数为0
		}
	}
	return s>=k;//返回条件!!!!
}

int main()
{
    
    
    
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>treelen[i];
		r=max(r,treelen[i]);//存放最大值
	}
	
	while(l<=r)//简单的对半查找
	{
    
    
		int mid=(l+r)/2;
		if(check(mid))
		{
    
    ;
			l=;
		}
		else
		{
    
    
			r=;
		}
	}
  	cout<<ans;//完美输出
  	
    return 0;
}

Guess you like

Origin blog.csdn.net/Joseph_tony/article/details/108560804
cut