-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathangrychildren.cpp
99 lines (73 loc) · 1.97 KB
/
angrychildren.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
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 a fight between the children, he would like to pick K out of N packets such that the unfairness is minimized.
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
unfairness=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
unfairness+=abs(xi-xj)
abs(x) denotes absolute value of x.
Input Format
The first line contains an integer N.
The second line contains an integer K.
N lines follow each integer containing the candy in the ith packet.
Output Format
A single integer which will be minimum unfairness.
Constraints
2<=N<=10^5
2<=K<=N
0<= number of candies in each packet <=10^9
Sample Input
7
3
10
100
300
200
1000
20
30
Sample Output
40
Explanation
Bill Gates will choose packets having 10, 20 and 30 candies.So unfairness will be |10-20| + |20-30| + |10-30| = 40. We can verify that it will be minimum in this way.
*/
#include <bits/stdc++.h>
using namespace std;
long go(vector<long> candies, long n, long k){
//size is n+1
sort(candies.begin(), candies.end());
long sum = 0;
long buffer = 0;
for (long i = 1; i <=k; ++i)
{
sum += i*candies[i];
sum -= (k-i+1)*candies[i];
buffer += candies[i];
}
long ans = sum;
for (long i = 2; i <= n-k+1; ++i)
{
sum = sum - 2*(buffer - candies[i-1])+(k-1)*candies[i-1]+(k-1)*candies[i+k-1];
buffer = buffer - candies[i-1]+candies[i+k-1];
ans = min(ans, sum);
}
return ans;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
long n, k;
cin>>n>>k;
vector<long> candies(n+1, 0);
for (int i = 1; i <= n; ++i)
{
cin>>candies[i];
}
cout << go(candies, n, k) << '\n';
return 0 ;
}