-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAggressive Cows.cpp
74 lines (64 loc) · 1.85 KB
/
Aggressive Cows.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
/* Hacker Blocks */
/* Title - Aggressive Cows */
/* Created By - Akash Modak */
/* Date - 27/06/2020 */
// Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).
// His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ wants to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
// Input Format
// First line contains N and C, separated by a single space, representing the total number of stalls and number of cows respectively. The next line contains N integers containing the indexes of stalls.
// Constraints
// 2 <= N <= 100,000
// 0 <= xi <= 1,000,000,000
// 2 <= C <= N
// Output Format
// Print one integer: the largest minimum distance.
// Sample Input
// 5 3
// 1 2 8 4 9
// Sample Output
// 3
// Explanation
// Problem Credits - (Spoj)[http://www.spoj.com/problems/AGGRCOW/]
#include<iostream>
#include<algorithm>
using namespace std;
bool checkCows(int a[],int n,int c,int minSep){
int lastCow=a[0];
int count = 1;
for(int i=1;i<n;i++){
if(a[i]-lastCow>=minSep){
lastCow = a[i];
count++;
if(count==c)
return true;
}
}
return false;
}
void search(int n,int c,int a[]){
int ans = 0;
int start = a[0];
int end = a[n-1] - a[0];
while(start<=end){
int mid = (start+end)/2;
bool check = checkCows(a,n,c,mid);
if(check){
start = mid+1;
ans= mid;
}
else
end=mid-1;
}
cout<<ans<<endl;
}
int main() {
int n,c;
cin>>n>>c;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
search(n,c,a);
return 0;
}
© 2020 GitHub, Inc.