Points chocolate - Algorithms small note - the eighth province Blue Bridge Cup competition C ++ A / Group B

Description Title
Here Insert Picture Description
Input Sample
2 10
. 6. 5
. 5. 6
Output Sample
2

AC Code

/*
整体思路:
其实只需要尝试即可,利用二分去尝试这个边长的蛋糕是否可以切出大于小朋友数目的块数即可 
*/ 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int K, N;
int H[100005], W[100005];

bool judge(int size)
{
	int _count = 0;
	for (int i = 0; i < N; i++)
	{
		_count += (int)(H[i] / size)*(int)(W[i] / size);//计算这个能切多少块 
		if (_count >= K) return true;
	}
	return false;
}

int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; ++i)
		scanf("%d %d", &H[i], &W[i]); 

	//二分查找出合适的边长
	int left = 1;
	int right = 100000;
	while (right > left)
	{
		int mid = (left + right + 1) / 2 ;//这里,因为left=mid,而不是mid+1,所以此处加1,防止死循环 
		if (judge(mid))
		{
			left = mid;
		}
		else
		{
			right = mid - 1;
		}

	}

	cout << left << endl;
	return 0;
}
Published 40 original articles · won praise 50 · views 4698

Guess you like

Origin blog.csdn.net/qq_43800455/article/details/105330420