WA#2#6#7

P2440 木材加工

wangruize88 @ 2024-09-16 11:48:05

#include <iostream>
using namespace std ;
typedef long long LL ;
LL a[100001] , k , l = 1 , r = 0 , n , sum ;
inline int fun ( LL m ) {
    int cnt = 0 ;
    for ( LL i = 1 ; i <= n ; i++ ) cnt += a[i]/m ;
    if ( cnt == k ) return 1 ;
    else if ( cnt > k ) return 2 ;
    else return 3 ;
}
int main () {
    cin >> n >> k ;
    for ( LL i = 1 ; i <= n ; i++ ) {
        cin >> a[i] ;
        sum += a[i] ;
        if ( a[i] > r ) r = a[i] ;
    }
    if ( sum < k ) {
        cout << 0 ;
        return 0 ;
    }
    while ( l < r ) {
        LL mid = l+((r-l)>>1) ;
        if ( fun(mid) == 2 ) l = mid+1 ;
        else if ( fun(mid) == 3 ) r = mid ;
        else {
            if ( fun(mid+1) != 1 ) {    
                cout << mid ;
                break ;
            } else {
                l = mid+1 ;
            }
        }
    }
    return 0 ;
}

|