티스토리 뷰


3개의 증가수열을 고르고 그 때의 비용이 최소를 찾는 문제다

N^2 DP로 풀었다


d[pos][cnt]로 테이블을 잡고 pos까지 봤고 cnt개 더 고를 수있을 때로 문제를 풀었다


문제 풀고 틀려서 보니 메모이제이션을 안했다 ㅎㅎ

소스 잘 봐야겠따..!


소스코드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#define ll long long
#define inf 1e17
using namespace std;
ll arr[3030], cost[3030];
int n;
ll d[3030][5];
 
ll go(ll pos, ll cnt) {
    if (cnt <= 0 || pos > n) return 0;
    if (cnt == 1return cost[pos];
 
    ll&ret = d[pos][cnt];
    if (ret != -1return ret;
    ret = inf;
    for (int i = pos+1; i <= n; i++) {
        if (arr[pos] < arr[i]) ret = min(ret, go(i, cnt-1+ cost[pos]);
    }
    return ret;
}
int main() {
    memset(d, -1sizeof(d));
    scanf(" %d"&n);
 
    for (int i = 1; i <= n; i++scanf(" %lld"&arr[i]);
    for (int i = 1; i <= n; i++scanf(" %lld"&cost[i]);
 
        ll ans = inf;
        for (int i = 1; i <= n; i++) ans = min(ans, go(i, 3));
 
        if (ans == inf) ans = -1;
        printf("%lld\n", ans);
    
    
}
cs


'알고리즘 > Codeforces' 카테고리의 다른 글

Edu82(Div.2) C. Perfect Keyboard  (0) 2020.02.16
Edu82(Div.2) B. National Project  (0) 2020.02.16
R495(Div.2) C. Sonya and Robots  (0) 2018.07.06
R485(Div.2) D. Fair  (0) 2018.05.30
R479(Div.3) E. Cyclic Components  (0) 2018.05.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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 31
글 보관함