• 愤怒的牛


    题目描述

    思路

    思路

    #include <cstdio>
    
    const int inf = 0x3f3f3f3f;
    int n, m, ans, L = inf, R = 0, arr[100005];
    inline int read() {
    	int s = 0, f = 1;
    	char ch = getchar();
    	while (ch < '0' || ch > '9') {
    		if (ch == '-') f = -1;
    		ch = getchar();
    	}
    	while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    	return s * f;
    }
    void qsort(int L, int R) {
    	int l = L, r = R;
    	int mid = arr[l + r >> 1], tmp;
    	while (l < r) {
    		while (arr[l] < mid) l++;
    		while (arr[r] > mid) r--;
    		if (l <= r) {
    			tmp = arr[l];
    			arr[l] = arr[r];
    			arr[r] = tmp;
    			l++, r--;
    		}
    	}
    	if (L < r) qsort(L, r);
    	if (l < R) qsort(l, R);
    }
    
    int main() {
    	n = read(), m = read();
    	for (int i = 1; i <= n; ++i) arr[i] = read();
    	qsort(1, n);
    	int l = 1, r = arr[n];
    	while (l <= r) {
    		int mid = l + r >> 1, cnt = 0, pre;
    		for (int i = 1; i <= n; ++i) {
    			if (i == 1) {
    				cnt = 1; pre = arr[1];
    			} else if (arr[i] - pre >= mid){
    				pre = arr[i], cnt++;
    			}
    		}
    		if (cnt >= m) {
    			ans = mid; l = mid + 1;
    		} else r = mid - 1;
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    012 spring retry重试原理的解析
    011 @Retryable的使用
    010 @ControllerAdvice
    009 SpringBoot+Swagger的使用
    008 @Import作用
    007 SpringBoot的@EnableAutoConfiguration注解
    001 品牌管理案例
    000 vue各种基本指令
    013 JS
    002 docker基本的命令
  • 原文地址:https://www.cnblogs.com/liuzz-20180701/p/11563668.html
Copyright © 2020-2023  润新知