<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=Angry_Children</id>
	<title>Angry Children - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=Angry_Children"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Angry_Children&amp;action=history"/>
	<updated>2026-05-14T05:40:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Angry_Children&amp;diff=838&amp;oldid=prev</id>
		<title>Eddy: Created page with &quot;=Problem Statement= Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and would like to distribute one packet to each of t...&quot;</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Angry_Children&amp;diff=838&amp;oldid=prev"/>
		<updated>2014-12-01T23:25:49Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=Problem Statement= Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and would like to distribute one packet to each of t...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Problem Statement=&lt;br /&gt;
Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and would like to distribute one packet to each of the K children in the village (each packet may contain different number of candies). To avoid any fighting among the children, he would like to pick K out of N packets, such that unfairness is minimized.&lt;br /&gt;
&lt;br /&gt;
Suppose the K packets have (x1, x2, x3,....xk) candies in them, where xi denotes the number of candies in the ith packet, then we define unfairness as&lt;br /&gt;
&lt;br /&gt;
max(x1,x2,...xk) - min(x1,x2,...xk)&lt;br /&gt;
&lt;br /&gt;
where max denotes the highest value amongst the elements, and min denotes the least value amongst the elements. Can you figure out the minimum unfairness and print it?&lt;br /&gt;
&lt;br /&gt;
===Input Format===&lt;br /&gt;
The first line contains an integer N.&lt;br /&gt;
The second line contains an integer K. N lines follow. Each line contains an integer that denotes the candy in the ith packet.&lt;br /&gt;
&lt;br /&gt;
===Output Format===&lt;br /&gt;
An integer that denotes the minimum possible value of unfairness.&lt;br /&gt;
&lt;br /&gt;
===Constraints===&lt;br /&gt;
1&amp;lt;=N&amp;lt;=105&lt;br /&gt;
1&amp;lt;=K&amp;lt;=N&lt;br /&gt;
0&amp;lt;= number of candies in any packet &amp;lt;=109&lt;br /&gt;
&lt;br /&gt;
===Sample Input #00===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
7&lt;br /&gt;
3&lt;br /&gt;
10&lt;br /&gt;
100&lt;br /&gt;
300&lt;br /&gt;
200&lt;br /&gt;
1000&lt;br /&gt;
20&lt;br /&gt;
30&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sample Output #00===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
20&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Explanation #00===&lt;br /&gt;
Here K = 3. We can choose packets that contain 10,20,30 candies. The unfairness is&lt;br /&gt;
&lt;br /&gt;
*max(10,20,30) - min(10,20,30) = 30 - 10 = 20&lt;br /&gt;
&lt;br /&gt;
===Sample Input #01===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
10&lt;br /&gt;
4&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
10&lt;br /&gt;
20&lt;br /&gt;
30&lt;br /&gt;
40&lt;br /&gt;
100&lt;br /&gt;
200&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
===Sample Output #01===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
3&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Explanation #01===&lt;br /&gt;
Here K = 4. We can choose the packets that contain 1,2,3,4 candies. The unfairness is&lt;br /&gt;
&lt;br /&gt;
*max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3&lt;br /&gt;
&lt;br /&gt;
==Soluzione di Eddy==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define MAX 100000&lt;br /&gt;
#define MAX_VAL 1000000001&lt;br /&gt;
&lt;br /&gt;
int candies[MAX];&lt;br /&gt;
&lt;br /&gt;
void MergeSort (int *candies, int primo, int ultimo);&lt;br /&gt;
void Merge (int *candies, int primo, int ultimo, int mezzo);&lt;br /&gt;
int Unfairness (int *candies, int len, int k);&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
&lt;br /&gt;
		int N,K;&lt;br /&gt;
		int i;&lt;br /&gt;
		FILE *fin;&lt;br /&gt;
&lt;br /&gt;
		if ((fin=fopen(&amp;quot;input.txt&amp;quot;,&amp;quot;r&amp;quot;)) == NULL)&lt;br /&gt;
			exit(2);&lt;br /&gt;
		fscanf(fin,&amp;quot;%d %d&amp;quot;,&amp;amp;N,&amp;amp;K);&lt;br /&gt;
		for(i = 0;i &amp;lt; N;i++)&lt;br /&gt;
				fscanf(fin,&amp;quot;%d&amp;quot;,candies + i);&lt;br /&gt;
		MergeSort (candies, 0, N-1);&lt;br /&gt;
		int unfairness = Unfairness (candies, N, K-1);&lt;br /&gt;
&lt;br /&gt;
		printf(&amp;quot;%d\n&amp;quot;,unfairness);&lt;br /&gt;
		return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* IDEA:&lt;br /&gt;
 * siccome l'array e` ordinato max = i-esima pos + (k-1), min = i-esima pos,&lt;br /&gt;
 * mi basta scorrere l'array pagando O(n), ma avevo ordinato: O(nlog(n))&lt;br /&gt;
 */&lt;br /&gt;
int Unfairness (int *candies, int len, int k)&lt;br /&gt;
{&lt;br /&gt;
		int i, min, tmp;&lt;br /&gt;
		i=0; min=candies[len-1];&lt;br /&gt;
		while (i+k &amp;lt; len){&lt;br /&gt;
				tmp = candies[i+k] - candies[i];&lt;br /&gt;
				if (tmp &amp;lt; min)  min = tmp;&lt;br /&gt;
				i++;&lt;br /&gt;
		}&lt;br /&gt;
		return min;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void MergeSort (int *candies, int primo, int ultimo)&lt;br /&gt;
{&lt;br /&gt;
		if (primo &amp;lt; ultimo){&lt;br /&gt;
				int mezzo = (primo+ultimo)/2;&lt;br /&gt;
				MergeSort(candies, primo, mezzo);&lt;br /&gt;
				MergeSort(candies, mezzo+1, ultimo);&lt;br /&gt;
				Merge(candies, primo, ultimo, mezzo);&lt;br /&gt;
		}    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void Merge (int *candies, int primo, int ultimo, int mezzo)&lt;br /&gt;
{&lt;br /&gt;
		int i, j, k, h;&lt;br /&gt;
		int tmp[MAX];&lt;br /&gt;
		i = primo; j = mezzo+1; k = primo;&lt;br /&gt;
		/* &amp;quot;compongo tmp&amp;quot; fino a quando non esaurisco una meta` */&lt;br /&gt;
		while (i &amp;lt;= mezzo &amp;amp;&amp;amp; j &amp;lt;= ultimo){&lt;br /&gt;
				if (candies[i]&amp;lt;=candies[j])&lt;br /&gt;
						tmp[k++] = candies[i++];&lt;br /&gt;
				else&lt;br /&gt;
						tmp[k++]=candies[j++];&lt;br /&gt;
		}&lt;br /&gt;
		j=ultimo;&lt;br /&gt;
		/*  se sono rimasti degli elementi nella prima meta` saranno i maggiori */&lt;br /&gt;
		for (h=mezzo; h&amp;gt;=i; h--)&lt;br /&gt;
				candies[j--]=candies[h];&lt;br /&gt;
		for (j=primo; j&amp;lt;=k-1; j++)&lt;br /&gt;
				candies[j]=tmp[j];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
[[User:Eddy|Eddy]] ([[User talk:Eddy|talk]]) 00:59 Mon, 02 Dic 2014 (CET)&lt;/div&gt;</summary>
		<author><name>Eddy</name></author>
	</entry>
</feed>