Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #define ll long long
- using namespace std;
- bool place(vector<ll> a, ll start, ll end, ll mid, ll c){
- if(c == 1)
- return true;
- if(c > (end - start + 1))
- {
- return false;
- }
- ll cow = 1;//assume one cow is placed at position 0;
- ll i = 0;
- while(cow <= c){
- // cout<<start<< " "<<end<<endl;
- // cout<<start;
- cout<<" "<<start<< "*"<<i<<endl;
- for( i = start + 1; i <= end; i++ ){
- if((a[i] - a[start]) >= mid){
- cow++;
- //another cow positioned at index i;
- start = i;
- cout<<" "<<start<< "*"<<i<<endl;
- // cout<<a[i]<<" "<<a[start]<<" "<<i<<endl;
- break;
- }
- }
- if(i >= end && cow <c)
- return false;
- else
- return true;
- }
- return true;
- }
- int main() {
- ll n, c;
- cin>>n>>c;
- vector<ll> a(n);
- for(ll i = 0; i < n; i++){
- cin>>a[i];
- }
- ll start = 0, end = n - 1, ans = 0;
- sort(a.begin(), a.end());
- // for(ll i = 0; i < n; i++){
- // cout<<a[i]<<" "<<"*"<<endl;
- // }
- while(start <= end){
- ll mid = (start + end)/2;
- if(place (a, 0, n -1, mid, c)){
- ans = mid;
- start = mid + 1;
- }
- else{
- end = mid - 1;
- }
- }
- // cout<<ans<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment