• POJ2456 Aggressive cows(二分)


    链接:http://poj.org/problem?id=2456

    题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最大值。

    思路:仍然是最大化最小值,套路一样,二分最小距离,每次check即可

    AC代码:

     1 #include<iostream>
     2 #include<vector>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<queue>
     8 #include<map>
     9 using namespace std;
    10 const int maxn = 1e5+5;
    11 typedef long long ll;
    12 ll sta[maxn];
    13 int N,C; 
    14 bool check(ll dis){
    15     int sum = 1;
    16     int cur = 0;
    17     for(int i = 1;i<N;i++){
    18         if(sta[i] - sta[cur]>=dis){
    19             sum++;
    20             cur = i;
    21         }
    22     }
    23     return sum>=C;
    24 }
    25 int main(){
    26     while(cin>>N>>C){
    27         for(int i = 0;i<N;i++){
    28             cin>>sta[i];
    29         }
    30         sort(sta,sta+N);
    31         ll MAX = -1;
    32         for(int i = 1;i<N;i++){
    33             MAX = max(MAX,sta[i]-sta[i-1]);
    34         }
    35         ll l = 0,r = MAX *2;
    36         ll mid;
    37         while(l<r){
    38             mid = (1+l+r)>>1;
    39             if(check(mid)){//sum>=C;
    40                 l = mid ;
    41             }
    42             else{
    43                 r = mid - 1;
    44             }
    45         }
    46     //    cout<<l<<" "<<r<<endl;
    47         cout<<l<<endl;
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    KOVN(ECC) / PRCD_ELEMENTS(HANA)
    金税 NWBC
    Vim使用方法
    Codeforces Round #744 (Div. 3) 题解
    Codeforces Round #769 (Div. 2) 题解
    vue3切换theme功能
    elementPlus使用elicon
    Vite2+Vue3+ts的eslint设置踩坑
    elementPlus配合vuerouter搭建后台系统菜单模块
    js判断字符串是否是中文
  • 原文地址:https://www.cnblogs.com/AaronChang/p/12172452.html
Copyright © 2020-2023  润新知