Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

dh-winternagi 님의 블로그

(2629) 양팔저울 본문

백준 (C++)/Solve

(2629) 양팔저울

dh-winternagi 2026. 4. 19. 23:00

https://www.acmicpc.net/problem/2629

단계별로 풀어보기

31단계(동적 계획법 2) 4번째

 

 

 

배낭 문제랑 똑같은데, 물건을 넣기만 할 수 있었던 가방 문제와 달리 양팔저울의 왼쪽 오른쪽 전부 추를 놓을 수 있다.

따라서 추를 더하는 경우 말고 빼는 경우도 계산해야 한다.

또한 계산 중간에 왼쪽이 더 무거운 경우가 생길수도 있으므로 이를 고려하여 범위를 잡아야 한다.

 

 

 

#include <iostream>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int n, m;
  
  cin >> n;
  
  vector<bool> dp(55001);
  
  dp[15000]= true;
  
  while(n--){
    int w;
    
    cin >> w;
    
    vector<int> res;
    
    for(int i=0;i<=55000;i++){
      if(dp[i])  res.push_back(i);
    }
    
    for(int x:res){
      if(x-w>=0)  dp[x-w]= true;
      if(x+w<=55000)  dp[x+w]= true;
    }
  }
  
  cin >> m;
  
  while(m--){
    int w;
    
    cin >> w;
    
    cout << (dp[w+15000] ? "Y " : "N ");
  }
  
  return 0;
}

'백준 (C++) > Solve' 카테고리의 다른 글

(7579) 앱  (0) 2026.04.19
(2293) 동전 1  (0) 2026.04.19
(1520) 내리막 길  (0) 2026.04.19
(11049) 행렬 곱셈 순서  (0) 2026.04.19
(11066) 파일 합치기  (0) 2026.04.19