기본 콘텐츠로 건너뛰기

[BOJ 2156]포도주 시식-DP조건 잘 생각하기

#include<cstdio>
#include<vector>
using namespace std;
int input[10001];
int output[10001];
int N;
int max(int a,int b,int c){
if (a>=b && a>=c)
return a;
else if(b>=a && b>=c)
return b;
return c;
}
int dp(){
output[0] = 0;
output[1] = input[1];
output[2] = output[1]+input[2];
for (int i=3;i<=N;i++){
int a,b,c;
a = output[i-1]; //현재를 먹지 않을경우
b = input[i]+output[i-2];//현재꺼 먹고 이전꺼 안먹는 경우
c = input[i]+input[i-1]+output[i-3]; //현재꺼 먹고 이전꺼도 먹는경우
output[i] = max(a,b,c);
}
return output[N];
}
int main(void)
{
scanf("%d",&N);
for (int i=1;i<=N;i++)
{
scanf("%d",&input[i]);
}
int result;
if (N == 1)
result = input[1];
else
result = dp();
printf("%d",result);
}
view raw [BOJ 2156] hosted with ❤ by GitHub

댓글

이 블로그의 인기 게시물

Tree traversal의 3가지

1. 전위 순회 (Preorder Traversal) Root -> Left Tree -> Right Tree   ( 루트를 제일 처음에 방문 ) 2. 중위 순회 (Inorder Traversal) Left Tree -> Root -> Right Tree   ( 루트를 중간에 방문 ) 3. 후위 순회 (Postorder Traversal) Left Tree -> Right Tree -> Root   ( 루트를 제일 마지막에 방문 ) <소스코드> 출처 : http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder /// C program for different tree traversals #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct node {      int data;      struct node* left;      struct node* right; }; /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct node* newNode(int data) {      struct node* node = (struct node*)                 ...