Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 스택
- 구현
- 우선순위 큐
- 백트래킹
- 재귀
- 문자열
- XR Interaction Toolkit
- 트리
- 유니온 파인드
- BFS
- 수학
- Team Fortress 2
- 브루트포스
- 다이나믹 프로그래밍
- Unreal Engine 5
- 유니티
- VR
- 투 포인터
- 자료구조
- 다익스트라
- 누적 합
- 그래프
- 그리디 알고리즘
- 정렬
- 백준
- c++
- ue5
- DFS
- 시뮬레이션
- 알고리즘
Archives
- Today
- Total
1일1알
백준 17610번 양팔저울 C++ 본문
https://www.acmicpc.net/problem/17610
17610번: 양팔저울
무게가 서로 다른 k개의 추와 빈 그릇이 있다. 모든 추의 무게는 정수이고, 그릇의 무게는 0으로 간주한다. 양팔저울을 한 번만 이용하여 원하는 무게의 물을 그릇에 담고자 한다. 주어진 모든 추
www.acmicpc.net
저울에 추를 올리는 경우 :
1. 추를 올리지 않는 경우
2. 왼쪽에 올리는 경우
3. 오른쪽으로 올리는 경우
추가 최대 13개이기때문에 dfs로 가능한 문제이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int k;
int maxSum = 0;
vector<int> v;
set<int> s;
void Dfs(int sum, int idx) {
if (idx == k) {
if (abs(sum) > maxSum) return;
if (sum == 0) return;
s.insert(abs(sum));
return;
}
Dfs(sum, idx + 1);
Dfs(sum + v[idx], idx + 1);
Dfs(sum - v[idx], idx + 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> k;
v = vector<int>(k);
for (int i = 0; i < k; i++) {
cin >> v[i];
maxSum += v[i];
}
Dfs(0, 0);
cout << maxSum - s.size();
}
'알고리즘' 카테고리의 다른 글
백준 1431번 시리얼 번호 C++ (0) | 2023.06.21 |
---|---|
백준 12101번 1, 2, 3 더하기 2 C++ (0) | 2023.06.10 |
백준 21278번 호석이 두 마리 치킨 C++ (1) | 2023.06.08 |
백준 3182번 한동이는 공부가 하기 싫어! C++ (0) | 2023.06.07 |
백준 19637번 IF문 좀 대신 써줘 C++ (1) | 2023.06.06 |