#include <bits/stdc++.h>
using namespace std;
int arr[20] , ans;
bool flag;
void rec(int l, int w, int h) {
if (l == 0 || w == 0 || h == 0) return;
for (int i = 19; i >= 0; i--) {
if (arr[i] == 0) continue;
int cur = 1 << i;
if (l >= cur && w >= cur && h >= cur) {
arr[i]--;
rec(l, w, h - cur);
rec(l-cur, w, cur);
rec(cur, w - cur, cur);
ans++;
return;
}
}
flag = true;
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int l, w, h, n;
cin >> l >> w >> h;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
arr[a] = b;
}
rec(l, w, h);
if (flag) {
cout << -1 << '\n';
}
else {
cout << ans << '\n';
}
return 0;
}
해당 과제를 처리할 수 있는 날 중 최대한 늦은 날을 찾아 이 과제를 하는 날로 정합니다.
예를 들어 과제가 4일 까지라면
4일, 3일, 2일, 1일을 보면서 이미 해당 날짜가 차지되어 있는지 확인하고
모두 이미 차지되었다면 이 과제를 할 수 없는 것입니다.
문제 풀이는 우선순위 큐를 이용해도 되고
벡터 정렬을 이용해도 됩니다.
2.문제풀이코드 C++
1.벡터정렬
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[1001];
struct Hw {
int day, point;
bool operator<(const Hw& b) const {
return point > b.point;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<Hw> v;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
v.push_back({ a,b });
}
sort(v.begin(), v.end());
int ans = 0;
for (int i = 0; i <v.size(); i++) {
int day = v[i].day;
int point = v[i].point;
while (ch[day] && day>=1) {
day--;
}
if (day == 0) continue;
ch[day] = 1;
ans += point;
}
cout << ans << '\n';
return 0;
}
2. 우선순위 큐 활용
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[1001];
struct Hw {
int day, point;
bool operator<(const Hw& b) const {
return point < b.point;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
priority_queue<Hw> Q;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
Q.push({ a,b });
}
int ans = 0;
while (!Q.empty()) {
int day = Q.top().day;
int point = Q.top().point;
Q.pop();
while (ch[day] && day>=1) {
day--;
}
if (day == 0) continue;
ch[day] = 1;
ans += point;
}
cout << ans << '\n';
return 0;
}
좌표는 중복된 값이 들어올 수 있으므로 set 자료구조를 이용하면 편하게 구할 수 있습니다.
6
2
1 6 9 3 6 7
백준 2212 설명
위 예제에서는 센서간의 간격의 길이가 3이 제일 길기 때문에
전체 간격의 길이인 9-1=8에서 3을 빼주면
나머지 센서를 다 포괄할 수 있게 됩니다.
2.문제풀이코드 C++
#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
set<int> num;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
num.insert(tmp);
}
n = num.size();
if (n <= k) {
cout << 0;
return 0;
}
vector<int> v(num.begin(), num.end());
vector<int> interval;
int ans = 0;
for (int i = 0; i < n - 1; i++) {
ans += v[i + 1] - v[i];
interval.push_back(v[i + 1] - v[i]);
}
sort(interval.begin(), interval.end());
for (int i = 0; i < k-1; i++) {
ans -= interval[interval.size() - 1 - i];
}
cout << ans << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[1000];
void compare(int x, int y, int z) {
for (int i = 123; i <= 987; i++) {
if (ch[i]) {
int s = 0, b = 0;
int arr1[3] = { i / 100, (i % 100) / 10 ,i % 10 };
int arr2[3] = { x / 100, (x % 100) / 10 ,x % 10 };
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if(j==k && (arr1[j]==arr2[k])){
s++;
}
else if (j != k && arr1[j] == arr2[k]) {
b++;
}
}
}
//후보에서 제외
if (!(y == s && z == b)) {
ch[i] = 0;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
//ch배열 초기화
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (i != j && k != j && i != k) {
ch[i * 100 + 10 * j + k] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
compare(x, y, z);
}
int ans = 0;
for (int i = 0; i < 1000; i++) {
if (ch[i]) ans++;
}
cout << ans << '\n';
return 0;
}
두번째 방법
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[101][1000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
int a, s, b;
cin >> a >> s >> b;
int x = a / 100;
int y = (a % 100) / 10;
int z = a % 10;
if (s == 3 && b == 0) {
cout << 1 << '\n';
return 0;
}
else if (s == 0 && b == 3) {
ch[i][z * 100 + x * 10 + y] = 1;
ch[i][y * 100 + z * 10 + x] = 1;
}
else if (s == 1 && b == 2) {
ch[i][100 * x + 10 * z + y] = 1;
ch[i][100 * z + 10 * y + x] = 1;
ch[i][100 * y + 10 * x + z] = 1;
}
else if (s == 2 && b == 0) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * y + j] = 1;
ch[i][100 * x + 10 * j + z] = 1;
ch[i][100 * j + 10 * y + z] = 1;
}
}
}
else if (s == 1 && b == 1) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * j + y] = 1;
ch[i][100 * x + 10 * z + j] = 1;
ch[i][100 * z + 10 * y + j] = 1;
ch[i][100 * j + 10 * y + x] = 1;
ch[i][100 * j + 10 * x + z] = 1;
ch[i][100 * y + 10 * j + z] = 1;
}
}
}
else if (s == 0 && b == 2) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * j + 10 * x + y] = 1;
ch[i][100 * y + 10 * x + j] = 1;
ch[i][100 * y + 10 * j + x] = 1;
ch[i][100 * z + 10 * j + x] = 1;
ch[i][100 * z + 10 * x + j] = 1;
ch[i][100 * j + 10 * z + x] = 1;
ch[i][100 * z + 10 * j + y] = 1;
ch[i][100 * y + 10 * z + j] = 1;
ch[i][100 * j + 10 * z + y] = 1;
}
}
}
else if (s == 1 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * x + 10 * j + k] = 1;
ch[i][100 * j + 10 * y + k] = 1;
ch[i][100 * j + 10 * k + z] = 1;
}
}
}
}
else if (s==0 && b == 1) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * j + 10 * x + k] = 1;
ch[i][100 * j + 10 * k + x] = 1;
ch[i][100 * y + 10 * j + k] = 1;
ch[i][100 * j + 10 * k + y] = 1;
ch[i][100 * z + 10 * j + k] = 1;
ch[i][100 * j + 10 * z + k] = 1;
}
}
}
}
else if (s == 0 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int t = 1; t <= 9; t++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z
&& x != t && y != t && z != t && k != t && j != t) {
ch[i][100 * j + 10 * t + k] = 1;
}
}
}
}
}
}
int ans = 0;
for (int i = 100; i < 1000; i++) {
bool flag = true;
for (int j = 0; j < n; j++) {
if (ch[j][i] == 0) {
flag = false;
break;
}
}
if (flag) ans++;
}
cout << ans << '\n';
return 0;
}