Skip to content

Commit c8a260a

Browse files
authored
Merge pull request ashutosh97#31 from imashwani/master
added question Aggressive cows spoj
2 parents 54b9b9d + e8f7fd5 commit c8a260a

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
AGGRCOW - Aggressive cows
2+
#binary-search
3+
4+
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).
5+
6+
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?
7+
Input
8+
9+
t � the number of test cases, then t test cases follows.
10+
* Line 1: Two space-separated integers: N and C
11+
* Lines 2..N+1: Line i+1 contains an integer stall ___location, xi
12+
Output
13+
14+
For each test case output one integer: the largest minimum distance.
15+
Example
16+
17+
Input:
18+
19+
1
20+
5 3
21+
1
22+
2
23+
8
24+
4
25+
9
26+
27+
Output:
28+
29+
3
30+
31+
Output details:
32+
33+
FJ can put his 3 cows in the stalls at positions 1, 4 and 8,
34+
resulting in a minimum distance of 3.

Aggressive Cows Spoj/solution1.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(NULL);
7+
long long t,n,c;
8+
cin>>t;
9+
vector<long long> ans;
10+
11+
while(t--){
12+
cin>>n>>c;
13+
long long x[n];
14+
long long mind=LLONG_MAX;
15+
for(long long i=0;i<n;i++){
16+
cin>>x[i];
17+
mind=min(x[i],mind);
18+
}
19+
sort(x,x+n);
20+
21+
long long low=mind;
22+
long long high=x[n-1]-x[0];
23+
long long mid;
24+
25+
while(low < high){
26+
mid = low+(high-low+1)/2;
27+
long long cows=1;
28+
long long idx=0;
29+
for(long long i=1;i<n;i++){
30+
if(x[i]-x[idx] >= mid) {cows++; idx=i;}
31+
}
32+
if(cows >= c) low=mid;
33+
else high=mid-1;
34+
}
35+
ans.push_back(low);
36+
}
37+
38+
for(vector<long long>::iterator it=ans.begin();it!=ans.end();it++){
39+
cout<<*it<<endl;
40+
}
41+
return 0;
42+
}

Aggressive Cows Spoj/solution2.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(NULL);
7+
long long t,n,c;
8+
cin>>t;
9+
vector<long long> ans;
10+
while(t--){
11+
cin>>n>>c;
12+
long long x[n];
13+
long long mind=LLONG_MAX;
14+
for(long long i=0;i<n;i++){
15+
cin>>x[i];
16+
mind=min(x[i],mind);
17+
}
18+
sort(x,x+n);
19+
20+
long long low=mind;
21+
long long high=x[n-1]-x[0];
22+
long long mid;
23+
long long maxd=-1;
24+
25+
while(low < high){
26+
mid = low+(high-low)/2;
27+
long long cows=1;
28+
long long idx=0;
29+
for(long long i=1;i<n;i++){
30+
if(x[i]-x[idx] >= mid) {cows++; idx=i;}
31+
}
32+
if(cows >= c) {low=mid+1; maxd=max(maxd,mid);}
33+
else high=mid;
34+
}
35+
ans.push_back(maxd);
36+
}
37+
38+
for(vector<long long>::iterator it=ans.begin();it!=ans.end();it++){
39+
cout<<*it<<endl;
40+
}
41+
return 0;
42+
}

0 commit comments

Comments
 (0)