Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提出 二分探索 #139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,37 @@ int n;
int k;
int A[100000];

//各成分を推定値で割ったときの切り上げの値を足す
int p(int m){
int sum = 0;
for(int j = 0; j < n; j++){
sum += (A[j] + m - 1) / m;
}
return (long long int) sum <= k;
}

//ubの値に代入するために配列の最大値を求める
int m(int B[]){
int max = 0;
for(int j = 0; j < n; j++){
if(max <= B[j]) max = B[j];
}
return max;
}

int main(){
int i, lb, ub;
scanf("%d%d", &n, &k);
for(i = 0; i < n; i++){
scanf("%d", &A[i]);
}


lb = 0;
ub = m(A);
while(ub - lb > 1){
int mid = (lb + ub) / 2;
if(p(mid)) ub = mid;
else lb = mid;
}
printf("%d\n", ub);
return 0;
}
11 changes: 8 additions & 3 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ int main(){
for(i = 0; i < n; i++){
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;
}
28 changes: 26 additions & 2 deletions src/spear.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@ int n;
int k;
int A[100000];

//各成分を推定値で割ったときの切り捨ての値を足す
int p(int m){
int sum = 0;
for(int j = 0; j < n; j++){
if(m != 0) sum += A[j] / m;
else sum = k + 1;
}
return (long long int) sum < k;
}

//ubの値に代入するために配列の成分の和を求める
int m(int B[]){
int max = 0;
for(int j = 0; j < n; j++){
if(max <= B[j]) max = B[j];
}
return max;
}

int main(){
int i, lb, ub;
scanf("%d%d", &n, &k);
for(i = 0; i < n; i++){
scanf("%d", &A[i]);
}


lb = 0;
ub = m(A);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初期値が不適切です.

while(ub - lb > 1){
int mid = (ub + lb) / 2;
if(p(mid)) ub = mid;
else lb = mid;
}
printf("%d\n", lb);
return 0;
}
47 changes: 42 additions & 5 deletions src/works.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int n;
int k;
int A[100000];
unsigned int n;
unsigned int k;
unsigned int A[100000];


int s(){
int sum = 0;
for(int j = 0; j < n; j++){
sum += A[j];
}
return sum;
}

int m(unsigned int B[]){
int max = 0;
for(int j = 0; j < n; j++){
if(max <= B[j]) max = B[j];
}
return max;
}

//人の入れ替わりがk-1回起こるようにする
int p(int m){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原因までは特定してないですが,資料中のケースでも正しく動いていないのでは?

int people = 1;
for(int a = 0; a < n-1; a++){
if(A[a] + A[a+1] <= m){
A[a+1] = A[a] + A[a+1];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aに直接操作するのはあまり良い方法ではないです.
何度もpを呼ぶわけなので.

}
else people++;
}
return (long long int) people <= k;
}

int main(){
int i, lb, ub;
scanf("%d%d", &n, &k);
for(i = 0; i < n; i++){
scanf("%d", &A[i]);
}


lb = m(A) - 1;
ub = s();
while(ub - lb > 1){
int mid = (lb + ub) / 2;
if(p(mid)) ub = mid;
else lb = mid;
}

printf("%d\n", ub);
return 0;
}