#include <iostream> using namespace std; class Stack { private: int Array[100000]; int Top; public: Stack(int n); void Push(int n); void Pop(); int GetTop(); int GetSize(); }; Stack::Stack(int n) { Top = 0; } void Stack::Push(int n) { Array[Top++] = n; } void Stack::Pop() { Top--; } int Stack::GetTop() { return Array[Top - 1]; } int Stack::GetSize() { return Top; } int main() { int N; cin >> N; Stack Data(0); bool Digit[100000]; for (int i = 0; i < 100000; i++) Digit[i] = true; int j_init = 1; for (int i = 0; i < N; i++) { int Temp; cin >> Temp; for (int j = j_init; j <= Temp; j++) { if (Digit[j - 1]) { Data.Push(j); Digit[j - 1] = false; cout << "+" << endl; } } for (int j = Data.GetTop(); j >= Temp; j--) { Data.Pop(); cout << "-" << endl; } j_init = Data.GetTop(); } }
The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house. You must use DP because you must use the previous value. Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time. If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house. Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem = https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE
댓글
댓글 쓰기