51Nod 2654 Minimum Distance Maximum c/c++ Problem Solution

Posted by BKPARTIES on Sat, 05 Oct 2019 23:21:50 +0200

Title Description

Given n n n positions, select k from them, so that the distance between the two adjacent positions of K is as large as possible, which means that the minimum value of k-1 distance is as large as possible. Output this maximum and minimum.
Sample explanation: Location: 159.
input
First line: 2 numbers N and k (2 <= n <= 100000, 2 <= k <= 10000, k <== n)
Later n rows: Pi is a number per line, indicating the specific location (0 <= Pi <= 10 ^ 9), which is disorderly.
output
Output a number corresponding to the maximum distance.
sample input
5 3
1
3
5
7
9
sample output
4

Explanation:

To find a distance as large as possible (1), and satisfy this distance is the smallest distance (2) of the k-1 distance selected from the n numbers.
The solution to point 1:
Enumeration distance can be either a direct enumeration of O(n) or a bipartite enumeration of O(logn). First look at the later time complexity to determine which one to use.
The solution to point 2:
From the enumeration of a distance x, to determine whether the distance x is the smallest distance in the number of k, is to see whether the smallest distance x can be taken from the number of n to extract the number of K (or > k), I do not understand here [to be understood].
This topic has a related topic in POJ.- Title Solution

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;
int n,k;
int p[MAX];

bool check(int x)
{
    int cnt = 1;
    int s = p[0];
    for(int i = 1; i < n; i++)
    {
        if(p[i] - s >= x)// The reason for ">=" is that x is only the smallest distance
        {
            cnt++;
            s = p[i];
        }
    }
    if(cnt >= k)// The reason for ">=" is that more than k numbers can also be used.
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    while(cin >> n >> k)
    {
        for(int i = 0; i < n; i++)
        {
            cin >> p[i];
        }
        sort(p,p+n);
        int l = 0;
        int r = p[n-1] - p[0];
        int res;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(check(mid))
            {
                res = mid;
                l = mid + 1;
            }
            else
            {
                r = mid - 1;
            }
        }
        cout << res << endl;
    }
    return 0;
}

Topics: iOS