강의

멘토링

커뮤니티

Inflearn Community Q&A

devbelly0745's profile image
devbelly0745

asked

Introduction to Algorithm Problem Solving for IT Employment (with C/C++): Coding Test Preparation

43. Music Video (Application of Binary Search: Decision Algorithm)

43번 뮤직비디오 오류 질문드립니다

Written on

·

212

1

테스트 케이스가

9 9 

1 2 3 4 5 6 7 8 9 

일때  정답이 9인거 같은데 선생님 코드는 1이 뜨는것 같아요

C++코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

네. 반례를 잡지 못했네요. 최소한 DVD 용량은 음악 한 개의 용량보다는 크거나 같아야 한다는 것을 놓쳤습니다.

코드를 다음과 같이 수정합니다. 영상은 바로 수정하겠습니다. 감사합니다.^^

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1001], n;
int Count(int s){
	int i, cnt=1, sum=0;
	for(i=1; i<=n; i++){
		if(sum+a[i]>s){
			cnt++;
			sum=a[i];
		}
		else sum=sum+a[i];
	}
	return cnt;
}
int main(){
	freopen("input.txt", "rt", stdin);
	int m, i, lt=1, rt=0, mid, res, maxx=-2147000000;
	scanf("%d %d", &n, &m);
	for(i=1; i<=n; i++){
		scanf("%d", &a[i]);
		rt=rt+a[i];
		if(a[i]>maxx) maxx=a[i];
	}
	while(lt<=rt){
		mid=(lt+rt)/2;
		if(mid>=maxx && Count(mid)<=m){	
			res=mid;
			rt=mid-1;
		}
		else lt=mid+1;
	}
	printf("%d\n", res);
	return 0;
}
devbelly0745's profile image
devbelly0745

asked

Ask a question