Blue Bridge Cup 2017 Group B Eighth C language problem solution provincial tournament exercises - exercises I. points chocolate **

Daily brushing title (26)

Blue Bridge Cup 8th C language group B match exercises Province

Problem I: points chocolate

Here Insert Picture Description
Here Insert Picture Description
First a look at this problem, the data quite big ah, 100000, and there are limited resources, a look likely to be a problem until the dynamic programming.
Title somewhat similar to the format of a question and input to the first input line and respectively the number of points to the number of children of the chocolate, the next few lines of each piece of chocolate are corresponding length and width. Here I would like to write a C code easy to understand

#include<stdio.h>

int main()
{
	int h[100000];
	int w[100000];
	int n, k;
	scanf("%d%d",&n, &k);
	int i;
	for(i = 0; i < n; i++)
	{
		scanf("%d%d",&h[i],&w[i]);
	}

	int cnt = 0, j;

	for(i = 100000; i >= 1; i--)
	{
		cnt = 0;
		for(j = 0; j < n; j++)
		{
			cnt += (h[j] / i) * (w[j] / i);
		}
		if(cnt >= k)
		{
			printf("%d\n",i);
			return 0;
		}		
	}	
	return 0;
} 

Here Insert Picture Description
As the title limits the size of the resource, so we need to optimize the code, using binary search method, as follows

#include<stdio.h>

int main()
{
	int h[100000], w[100000];
	int mid, l, r;
	l = 1;
	r = 100001;
	int cnt = 0, len = 0, i;
	int n, k;
	scanf("%d %d",&n, &k);
	for(i = 0; i < n; i++)
		scanf("%d %d",&h[i], &w[i]);
	while(l <= r)
	{
		cnt = 0;
		mid = (l + r) / 2;
		for(i = 0; i < n; i++)
		{
			cnt += (h[i] / mid) * (w[i] / mid);
		}
		if(cnt >= k)
		{
			l = mid + 1;
			len = mid;
		}
		else
		{
			r = mid - 1;
		}
	}
	printf("%d\n", len);
	return 0;
}

Results are as follows
Here Insert Picture Description

This question is to explain the purpose will be over here, if there is a need to explain the binary correlation algorithm to find, and you can deduct 1 point for articles like this in the comments section, if the thumbs up over 10 I will immediately talk about the dichotomy in the next issue algorithm to find, of course, I will say this knowledge after, just like the point of writing the words of blog updates will power will be even greater and faster. If you like my articles, please remember to triple Oh, praise concern the collection point, thanks for the support, the next issue is more exciting! ! !

Published 40 original articles · won praise 7 · views 3109

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/104683736