diff --git a/src/apple.c b/src/apple.c index 766d543..9f1d555 100644 --- a/src/apple.c +++ b/src/apple.c @@ -4,6 +4,7 @@ int n; int k; int A[100000]; +int p(int bag_accept); int main(){ int i, lb, ub; @@ -12,6 +13,31 @@ int main(){ scanf("%d", &A[i]); } + lb = 0; + ub = 1000000000; + while(ub - lb > 1){ + int mid = (lb + ub) / 2; + if(p(mid)){ + ub = mid; + } + else{ + lb = mid; + } + } + + printf("%d\n", ub); return 0; } + +int p(int bag_accept){ + int i, bag_sum; + + bag_sum = 0; + for(i = 0; i < n; i++){ + bag_sum += (A[i] + bag_accept - 1) / bag_accept; + } + + if(bag_sum <= k) return 1; + else return 0; +} \ No newline at end of file diff --git a/src/array.c b/src/array.c index 13ed925..0ca439c 100644 --- a/src/array.c +++ b/src/array.c @@ -12,7 +12,15 @@ int main(){ scanf("%d", &A[i]); } + lb = -1; + ub = n; + while(ub - lb > 1){ + int mid = (lb + ub) / 2; + if(A[mid] >= k) ub = mid; + else lb = mid; + } + printf("%d\n", ub); return 0; } diff --git a/src/spear.c b/src/spear.c index 766d543..19a2c2b 100644 --- a/src/spear.c +++ b/src/spear.c @@ -4,6 +4,7 @@ int n; int k; int A[100000]; +int p(int spear_length); int main(){ int i, lb, ub; @@ -12,6 +13,31 @@ int main(){ scanf("%d", &A[i]); } + lb = 0; + ub = 1000000001; + while(ub - lb > 1){ + int mid = (lb + ub) / 2; + if(p(mid)){ + lb = mid; + } + else{ + ub = mid; + } + } + + printf("%d\n", lb); return 0; } + +int p(int spear_length){ + int spear_num = 0; + int i; + + for(i = 0; i < n; i++){ + spear_num += A[i] / spear_length; + } + + if(spear_num >= k) return 1; + else return 0; +} \ No newline at end of file