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
- IndexedTree
- Bridge
- mergesorttree
- KMP
- LIS
- backtracking
- SlidingWindow
- LCA
- DFS
- greedy
- BinarySearch
- ArticulationPoint
- Sweeping
- Implementation
- Tree
- 2-sat
- Flow
- Math
- MST
- FenwickTree
- TwoPointers
- Floyd
- Bellman-Ford
- BFS
- Dijkstra
- scc
- ShortestPath
- Union-Find
- topologicalsort
- DP
Archives
- Today
- Total
정리충의 정리노트
[백준] 3948: 홍준이의 친위대 본문
0. 문제 주소
https://www.acmicpc.net/problem/3948
3948번: 홍준이의 친위대
문제 홍준 왕국의 국왕 홍준이는 자신을 호위하는 N명의 친위대 병사가 있다. 병사들의 키는 모두 다르다. 홍준이는 그들을 일렬로 세울 때, 키 순서대로 세우는 것보다 맨 끝 두 병사를 제외한 �
www.acmicpc.net
1. 풀이
처음엔 1차원 dp 배열로 점화식을 세워보려고 했으나 실패했다.
탑다운으로 현재 위치에 서려는 사람 기준 남아있는 사람들 중에 이 사람보다 키가 작은 사람 / 큰 사람의 수와 함께
다음에는 이 사람보다 큰 사람이 와야 하는지 여부를 파라미터로 사용한다.
2. 풀이 코드
* 유의할 점
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[22][22][2];
ll solve(int L, int R, bool getting_higher){
if (L + R == 0) return 1;
ll &ret = dp[L][R][getting_higher];
if (ret != -1) return ret;
ret = 0;
if (!getting_higher){ // choose L
for (int next=0;next<L;next++){
// .....curr.....
// <-L-> <-R->
// ..next...curr.....
// choose next in L
ret += solve(next, (L+R) - (next+1), 1);
}
}
else{
for (int next=0;next<R;next++){
// .....curr.....
// <-L-> <-R->
// .....curr..next...
// choose next in R
ret += solve((L+R) - (next+1), next, 0);
}
}
return ret;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(dp, -1, sizeof(dp));
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d", &n);
if (n == 1) {
printf("1\n");
continue;
}
ll ans = 0;
for (int i=0;i<n;i++){
ans += solve(i, n - (i+1), 0);
ans += solve(i, n - (i+1), 1);
}
printf("%lld\n", ans);
}
return 0;
}
'PS > DP' 카테고리의 다른 글
[백준] 2618: 경찰차 (0) | 2020.02.29 |
---|---|
[백준] 10714: 케이크 자르기 2 (0) | 2020.02.29 |
[백준] 1103: 게임 (1) | 2020.02.03 |
[백준] 2624: 동전 바꿔주기 (0) | 2020.01.28 |
[백준] 1256: 사전 (0) | 2020.01.28 |
Comments