https://www.acmicpc.net/problem/1983
[난이도] Gold3
[유형] DP
[풀이]
DP
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int N,dp[400][400][400],v,inf=-8e7,as,bs;
vector<int> arr[2];
int sol(int n,int a,int b){
if(a==as||b==bs) return 0;
int& ret = dp[n][a][b];
if(ret!=inf) return ret;
ret=sol(n+1,a+1,b+1)+arr[0][a]*arr[1][b];
if(N>n+as-a) ret=max(ret,sol(n+1,a,b+1));
if(N>n+bs-b) ret=max(ret,sol(n+1,a+1,b));
return ret;
}
int main(){
scanf("%d",&N);
for(int j=0;j<2;j++){
for(int i=0;i<N;i++){
scanf("%d",&v);
if(v!=0) arr[j].push_back(v);
}
}
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
for(int k=0;k<N;k++) dp[i][j][k]=inf;
as=arr[0].size(),bs=arr[1].size();
printf("%d",sol(0,0,0));
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold3/1983.cpp
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold2] 16946 : 벽 부수고 이동하기4 (C++) (0) | 2021.03.15 |
---|---|
[BOJ/백준][Gold3] 12785 : 토쟁이의 등굣길 (C++) (0) | 2021.03.01 |
[BOJ/백준][Gold3] 17616 : 등수 찾기 (C++) (0) | 2021.03.01 |
[BOJ/백준][Gold3] 2836 : 수상 택시 (C++) (0) | 2021.03.01 |
[BOJ/백준][Gold5] 2170 : 선 긋기 (C++) (0) | 2021.03.01 |