PangLog
PangLog_k.k
PangLog
전체 방문자
오늘
어제
  • Category (77)
    • 💾기록 (2)
      • 📔기록 (2)
    • 🔧알고리즘 (10)
    • ⚡AI (17)
      • ∃Mathematics (11)
      • AI (5)
      • 논문 (1)
    • 👨‍💻Data Science (2)
    • 📚CS (4)
      • 📡컴퓨터 네트워크 (3)
      • 💾DB (0)
      • ⚙OS (1)
    • ⌨Programming (15)
      • Python (6)
      • Pytorch (3)
      • FastAPI (0)
      • Java (1)
      • Spring (3)
      • Elastic Search (2)
    • 💻 (23)
      • Git (9)
      • Issue sol (2)
      • Linux (2)
      • etc (7)
      • Web (2)
      • Docker (1)
    • 📰칼럼 (4)
      • IT (4)
      • 그 외 (0)
    • Review (0)

블로그 메뉴

  • 홈
  • Github

인기 글

최근 글

태그

  • pycham
  • 프로그래머스
  • 인퍼런스
  • 외부단편화
  • 백준
  • 쥬피터랩
  • URL URI 차이
  • 11660
  • K-디지털트레이닝 해커톤
  • 백준허브 이슈
  • 내부단편화
  • Jupyter Lab
  • 자바
  • 백준허브 에러
  • 깃허브
  • 백준허브
  • 알고리즘
  • 옹알이(1)
  • Python
  • Java
  • BOJ
  • SWEA
  • 5215
  • inference
  • 프로그래머스 체육복
  • cors
  • 파이참
  • 파이썬
  • 탐색적 데이터 분석
  • cv2
hELLO · Designed By 정상우.
PangLog

PangLog_k.k

🔧알고리즘

[swea] [Java] 5215. 햄버거 다이어트

2022. 8. 11. 18:09

[문제링크]

 

주어진 만족도, 칼로리 쌍을 이용하여 최대 칼로리 이하에서 최대의 만족도를 구하는 문제이다

 

✨IDEA : 

낮은 칼로리 순으로 정렬 후 합쳐볼까..

높은 만족도 순으로 정렬 후 합쳐볼까... 어떻게 조합을 해볼까 고민이 될수도 있지만 그냥 모든 조합들을 다 생각하면 된다

즉, 해당 쌍을 포함한 것 안포함한 것의 모든 경우를 계산한 뒤 주어진 칼로리 제한 안에서 가장 높은 만족도를 가지는 것을 출력!

 

처음 buger메소드를 구현할 때는 cnt가 주어진 K번까지 부분집합 경우를 체크하고 최댓값을 체크했으나 그럴필요 없이 재귀함수가 돌아가는 중 제한된 칼로리 보다 높아지면 바로 짤라주면 더 효율적인 풀이가 될 수 있다

package SWEA;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.StringTokenizer;

public class 햄버거 {
	static int T, N, limit, answer;
	static int [][] arr; //칼로리, 만족도를 담을 배열
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringBuilder output = new StringBuilder();
	static StringTokenizer tokens;
	
	static void buger(int cnt, int good, int cal) {
		if(cal > limit) // 주어진 칼로리 제한보다 높아지면 바로 컽!
			return;
		if(cnt == N) {
			answer = Math.max(answer, good);
			return;
		}
		
		buger(cnt + 1, good, cal);
		buger(cnt + 1, good + arr[cnt][0], cal + arr[cnt][1]);
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		input = new BufferedReader(new FileReader("./input.txt"));
		
		T = Integer.parseInt(input.readLine());
		for(int tc = 1; tc <= T; tc++) {
			tokens = new StringTokenizer(input.readLine());
			N = Integer.parseInt(tokens.nextToken());
			limit = Integer.parseInt(tokens.nextToken());
			
			arr = new int[N][2];
			
			for(int i = 0; i < N; i++) {
				tokens = new StringTokenizer(input.readLine());
				arr[i][0] = Integer.parseInt(tokens.nextToken());
				arr[i][1] = Integer.parseInt(tokens.nextToken());
			}
			
			answer = Integer.MIN_VALUE;
			buger(0, 0, 0);
			
			output.append(String.format("#%s %d\n", tc, answer));
		}
		System.out.println(output);
		
		
	}

}
반응형
저작자표시 비영리 변경금지 (새창열림)

'🔧알고리즘' 카테고리의 다른 글

[BOJ,백준][파이썬] 16918, 봄버맨  (0) 2023.04.04
[프로그래머스][파이썬] 옹알이(1)  (0) 2022.12.19
[BOJ][파이썬, 자바]11660 구간 합 구하기5  (0) 2022.08.03
[프로그래머스]체육복, 파이썬(python)  (0) 2022.06.09
[swea]1204. 최빈수 구하기, 파이썬(python)  (0) 2022.05.26
    '🔧알고리즘' 카테고리의 다른 글
    • [BOJ,백준][파이썬] 16918, 봄버맨
    • [프로그래머스][파이썬] 옹알이(1)
    • [BOJ][파이썬, 자바]11660 구간 합 구하기5
    • [프로그래머스]체육복, 파이썬(python)
    PangLog
    PangLog

    티스토리툴바