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 |
Tags
- 게리멘더링2
- dfs
- AVL 시간 복잡도
- 스택의 특징
- 원판 돌리기
- 풀이
- Stack 이란
- 백준 1158
- 1764
- 버킷 정렬
- 자료구조
- 백준
- 백준 1406
- 백준 17822
- C/C++ 구현
- 해시 구현
- 백준 2447
- heap
- 5397
- 구현
- c#
- 조세퍼스 순열
- 백준 5397
- 별 찍기 10
- ㅣ풀이
- 백준 17779
- 해시구현
- 백준 17471
- qorwns
- 시간 복잡도
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 9465] 스티커 본문
요구사항
스티커의 조건이 주어졌을때, 스티커의 합 최대로 구하기
풀이
n이 100,000이니깐 완전 탐색으로는 힘들다
스티커를 고르는 조건은 2개이다
현재 자리 고르고 다음 칸의 반대편 층 수 고르기
and
현재 자리 고르고 다다음 칸의 같은 층 수 고르기
n-1칸의 1층, 2층 중 최대값이 dp배열에 저장된다
#pragma warning(disable:4996)
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int d[2][100100];
int map[2][100100];
int n;
int func(int floor, int pos) {
if (pos < 0) return 0;
if (pos == 0) {
if (floor == 1) return map[floor][pos];
else return map[floor][pos];
}
int &ref = d[floor][pos];
if (ref != -1) return d[floor][pos];
if (floor == 0) ref = map[floor][pos] + max(func(1, pos - 1), func(1, pos - 2));
else ref = map[floor][pos] + max(func(0, pos - 1), func(0, pos - 2));
return ref;
}
int main() {
int t;
scanf("%d", &t);
for (int tc = 1; tc <= t; tc++) {
int n;
scanf("%d", &n);
memset(d, -1, sizeof(d));
for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) scanf("%d", &map[i][j]);
int ans = max(func(1, n - 1), func(0, n - 1));
cout << ans << endl;
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2251] 물통 (0) | 2020.03.18 |
---|---|
[백준 2294] 동전 2 (0) | 2020.03.17 |
[백준 1003] 피보나치 함수 (0) | 2020.03.16 |
[백준 1463] 1로 만들기 (0) | 2020.03.16 |
[백준 9370] 미확인 도착지 (0) | 2020.03.16 |
Comments