#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int T, N;
int dp[1010];
int arr[1010];
 
int main(void)
{
//    freopen("B10211_input.txt", "r", stdin);
    
    cin >> T;
    
    while(T--)
    {
        cin    >> N;
        
        for(int i = 1; i <= N; i++)
        {
            cin >> arr[i];
        }
        
        int Max = -99999999;
 
        for(int i = 1; i <= N; i++)
        {
            dp[i] = max(0, dp[i-1]) + arr[i];
            Max = max(Max, dp[i]);
        }
        
        cout << Max << endl;
    }
    
    return 0;
}
cs
#include <iostream>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
 
int cost[1010][1010];
int dp[1010][1010][4]; // 0 : 왼쪽, 1 : 중앙, 2 : 오른쪽 
int row, col;
 
int solve(int x, int y, int dir)
{
    if(x == row)
    {
        return 0;
    }
    
    if(dp[x][y][dir] != 99999999)
    {
        return dp[x][y][dir];
    }
    
    // 왼쪽
    if(dir != 0 && y-1 >= 0)
    {
        dp[x][y][dir] = solve(x+1, y-10+ cost[x][y];     
    } 
    
    // 중앙 
    if(dir != 1)
    {
        dp[x][y][dir] = min(dp[x][y][dir], solve(x+1, y, 1+ cost[x][y]);    
    }
    
    // 오른쪽
    if(dir != 2 && y+1 < col)
    {
        dp[x][y][dir] = min(dp[x][y][dir], solve(x+1, y+12+ cost[x][y]); 
    } 
    
    return dp[x][y][dir];
}
 
int main(void)
{
//    freopen("B17485_input.txt", "r", stdin);
    
    cin >> row >> col;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> cost[i][j];
            
            for(int k = 0; k < 4; k++)
            {
                dp[i][j][k] = 99999999;    
            }
        }
    }
     
    int Min = 99999999;
    for(int i = 0; i < col; i++)
    {
        // 처음에는 방향이 없기 때문에 dir에 3을 대입 
        Min = min(Min, solve(0, i, 3));
    }
    
    cout << Min;
    
    return 0;
}
cs
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int N, K;
int dp[101][100001];
int w[101];
int v[101];
 
int solve(int item, int capacity)
{
    if(item >= N)
    {
        return 0;
    }
    
    if(dp[item][capacity] != 0)
    {
        return dp[item][capacity];
    }
    
    // 배낭에 넣지 않는 경우 
    dp[item][capacity] = solve(item+1, capacity);     
    
    // 배낭에 넣는 경우 
    if(capacity-w[item] >= 0)
    {
        dp[item][capacity] = max(dp[item][capacity], solve(item+1, capacity-w[item]) + v[item]);    
    } 
    
    return dp[item][capacity]; 
}
 
int main(void)
{
//    freopen("B12865_input.txt", "r", stdin);
    
    cin >> N >> K;
    
    for(int i = 0; i < N; i++)
    {
        cin >> w[i] >> v[i];    
    }
    
    cout << solve(0, K);
    
    return 0;
}
cs
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int N, M, C;
int jewel[15]; 
int dp[12][1<<13][22]; // [현재가방][챙긴보석][남은한도] 
 
int solve(int cur, int visited, int capacity)
{
    if(cur == M)
    {
        return 0;
    }
    
    if(dp[cur][visited][capacity] != 0)
    {
        return dp[cur][visited][capacity];
    } 
    
    for(int i = 0; i < N; i++)
    {
        if(visited & (1 << i))
        {
            continue;
        }    
        
        if(capacity < jewel[i])
        {
            dp[cur][visited][capacity] = max(dp[cur][visited][capacity], solve(cur+1, visited, C));    
        }
        else 
        {
            dp[cur][visited][capacity] = max(dp[cur][visited][capacity], solve(cur, visited | (1 << i), capacity - jewel[i]) + 1);
        }
    } 
    
    return dp[cur][visited][capacity];
}
 
int main(void)
{
//    freopen("B1480_input.txt", "r", stdin);
    
    cin >> N >> M >> C;
    
    for(int i = 0; i < N; i++)
    {
        cin >> jewel[i];
    }
    
    cout << solve(00, C);
    
    return 0;
}
cs
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int dist[20][20];
int dp[20][1<<16]; // [현재도시][방문한도시]
int N;
 
int solve(int cur, int visited)
{
    // 모든 마을 방문시 
    if(visited == (1 << N) - 1)
    {
        // 0번 마을로 다시 돌아갈 수 있을때 
        if(dist[cur][0!= 0)
        {
            return dist[cur][0];
        }
        // 그 코스는 불가능 하도록 만들기 위해 매우 큰 수를 return
        else
        {
            return 99999999;    
        }
    }
    
    if(dp[cur][visited] != -1)
    {
        return dp[cur][visited];
    } 
    
    dp[cur][visited] = 99999999;
    
    for(int i = 1; i < N; i++)
    {
        // i번째 지역을 방문한 경우 
        if(visited & (1 << i))
        {
            continue;
        }    
        
        // 해당 지역의 비용이 0인 경우 
        if(dist[cur][i] == 0)
        {
            continue;
        }
        
        dp[cur][visited] = min(dp[cur][visited], solve(i, visited | (1 << i)) + dist[cur][i]);
    } 
    
    return dp[cur][visited];
}
 
int main(void)
{
//    freopen("B2098_input.txt", "r", stdin);
    
    cin >> N; 
    
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            cin >> dist[i][j];    
        }
    }
    
    memset(dp, -1sizeof(dp));
    
    // 0 →1 →2 →3 →4 →0  시작점이 0인 최소비용 루트가 있다고 가정해보자. 이 때 'A원'의 비용이 들었다면, 
    // 2 →3 →4 →0 →1 →2  시작점이 2인 최소비용 루트는 시작점과 관계없이 'A원'의 비용이 든다. 
    cout << solve(01);
    
    return 0;
}
cs
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
 
vector<vector<int>> Map;
map<pair<pair<intint>pair<intint>>int> visited;
int n;
int answer;
 
int dx[4= {-1100};
int dy[4= {00-11};
 
int safe(int x, int y)
{
    if(0 <= x && x < n && 0 <= y && y < n)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
int check(int x1, int y1, int x2, int y2, int order, int shape)
{   
    // 가로
    if(shape == 0)
    {
        if(order == 0 || order == 2)
        {
            if(safe(x1-1, y1) == 0 || safe(x2-1, y2) == 0)
            {
                return 0;
            }
 
            if(Map[x1-1][y1] != 0 || Map[x2-1][y2] != 0)
            {
                return 0;
            }
        }
        else if(order == 1 || order == 3)
        {
            if(safe(x1+1, y1) == 0 || safe(x2+1, y2) == 0)
            {
                return 0;
            }
 
            if(Map[x1+1][y1] != 0 || Map[x2+1][y2] != 0)
            {
                return 0;
            }
        }   
    }
    // 세로
    else
    {
        if(order == 0 || order == 2)
        {
            if(safe(x1, y1-1== 0 || safe(x2, y2-1== 0)
            {
                return 0;
            }
 
            if(Map[x1][y1-1!= 0 || Map[x2][y2-1!= 0)
            {
                return 0;
            }
        }
        else if(order == 1 || order == 3)
        {
            if(safe(x1, y1+1== 0 || safe(x2, y2+1== 0)
            {
                return 0;
            }
 
            if(Map[x1][y1+1!= 0 || Map[x2][y2+1!= 0)
            {
                return 0;
            }
        }     
    }
 
   return 1
 
void BFS()
{
    queue<pair<pair<intint>pair<intint>>> q;
    q.push({{00}, {01}});
    visited[{{00}, {01}}] = 1;
    visited[{{01}, {00}}] = 1;
    
    while(!q.empty())
    {
        int cnt = q.size();
        
        while(cnt--)
        {
            int x1 = q.front().first.first;
            int y1 = q.front().first.second;
            int x2 = q.front().second.first;
            int y2 = q.front().second.second;
            q.pop();
 
            // (x1, y1), (x2, y2) 순서 유지
            if(x2 < x1)
            {
                int temp = x1;
                x1 = x2;
                x2 = temp;
            }
 
            if(y2 < y1)
            {
                int temp = y1;
                y1 = y2;
                y2 = temp;
            } 
 
            // 도착 지점 종료
            if(x2 == n-1 && y2 == n-1)
            {
                return;
            }
 
            // 이동
            for(int i = 0; i < 4; i++)
            {        
                int xpos1 = x1 + dx[i];
                int ypos1 = y1 + dy[i];
                int xpos2 = x2 + dx[i];
                int ypos2 = y2 + dy[i];
 
                if(safe(xpos1, ypos1) == 1 && safe(xpos2, ypos2) == 1)
                {
                    if(visited[{{xpos1, ypos1}, {xpos2, ypos2}}] == 0 && visited[{{xpos2, ypos2}, {xpos1, ypos1}}] == 0 && Map[xpos1][ypos1] == 0 && Map[xpos2][ypos2] == 0)
                    {
                        visited[{{xpos1, ypos1}, {xpos2, ypos2}}] = visited[{{x1, y1}, {x2, y2}}] + 1;
                        visited[{{xpos2, ypos2}, {xpos1, ypos1}}] = visited[{{x2, y2}, {x1, y1}}] + 1;
 
                        q.push({{xpos1, ypos1}, {xpos2, ypos2}});
                    }
                }       
            }
 
            // 회전
            for(int i = 0; i < 4; i++)
            {
                int xpos1, ypos1, xpos2, ypos2;
 
                // 가로  
                if(abs(y2-y1) == 1)
                {
                    if(check(x1, y1, x2, y2, i, 0== 0)
                    {
                       continue;   
                    }
 
                    // x1, y1 기준 위로
                    if(i == 0)
                    {
                        xpos1 = x1;
                        ypos1 = y1;
                        xpos2 = x1-1;
                        ypos2 = y1;
                    }
                    // x1, y1 기준 아래로 
                    else if(i == 1)
                    {
                        xpos1 = x1;
                        ypos1 = y1;
                        xpos2 = x1+1;
                        ypos2 = y1;
                    }
                    // x2, y2 기준 위로
                    else if(i == 2)
                    {
                        xpos1 = x2;
                        ypos1 = y2;
                        xpos2 = x2-1;
                        ypos2 = y2;
                    }
                    // x2, y2 기준 아래로
                    else if(i == 3)
                    {
                        xpos1 = x2;
                        ypos1 = y2;
                        xpos2 = x2+1;
                        ypos2 = y2;
                    }
                }
                // 세로 
                else if(abs(x2-x1) == 1)
                {
                    if(check(x1, y1, x2, y2, i, 1== 0)
                    {
                       continue;   
                    }
 
                    // x1, y1 기준 좌로 
                    if(i == 0)
                    {
                        xpos1 = x1;
                        ypos1 = y1;
                        xpos2 = x1;
                        ypos2 = y1-1;
                    }
                    // x1, y1 기준 우로 
                    else if(i == 1)
                    {
                        xpos1 = x1;
                        ypos1 = y1;
                        xpos2 = x1;
                        ypos2 = y1+1;
                    }
                    // x2, y2 기준 좌로
                    else if(i == 2)
                    {
                        xpos1 = x2;
                        ypos1 = y2;
                        xpos2 = x2;
                        ypos2 = y2-1;
                    }
                    // x2, y2 기준 우로 
                    else if(i == 3)
                    {
                        xpos1 = x2;
                        ypos1 = y2;
                        xpos2 = x2;
                        ypos2 = y2+1;
                    }
                }
 
                if(visited[{{xpos1, ypos1}, {xpos2, ypos2}}] == 0 && visited[{{xpos2, ypos2}, {xpos1, ypos1}}] == 0)
                {
                    visited[{{xpos1, ypos1}, {xpos2, ypos2}}] = visited[{{x1, y1}, {x2, y2}}] + 1;
                    visited[{{xpos2, ypos2}, {xpos1, ypos1}}] = visited[{{x2, y2}, {x1, y1}}] + 1;
 
                    q.push({{xpos1, ypos1}, {xpos2, ypos2}});
                }
            }   
        }
        
        answer++;
    }                               
}
 
int solution(vector<vector<int>> board) 
{
    Map = board;
    n = board.size();
    
    BFS();
    
    return answer;
}
cs
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
int solution(int n, int m, vector<vector<int>> edge_list, int k, vector<int> gps_log) 
{
    int answer = 0;
    vector<int> adj[210];
    int dp[210][210];
    
    for(int i = 0; i < edge_list.size(); i++)
    {
        adj[edge_list[i][0]].push_back(edge_list[i][1]);
        adj[edge_list[i][1]].push_back(edge_list[i][0]);
    }
    
    for(int i = 0; i < 210; i++)
    {
        for(int j = 0; j < 210; j++)
        {
            dp[i][j] = 99999999;
        }
    }
    
    // dp[i][j] : 경로의 i번째 위치가 j번 도시가 되면서 i번째 위치까지의 경로가 valid하게 고쳐야 하는 최소 횟수
    dp[0][gps_log[0]] = 0;
    for(int i=1;i<k-1;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int add = (gps_log[i] == j ? 0 : 1);
            dp[i][j] = min(dp[i][j], dp[i-1][j] + add);
            
            for(auto p : adj[j])
            {
                dp[i][j] = min(dp[i-1][p] + add, dp[i][j]);
            }
        }
    }
    
    answer = 99999999;
    for(auto p : adj[gps_log.back()])
    {
        answer = min(answer, dp[gps_log.size()-2][p]);
    }
    
    if(answer > 100)
    {
        answer = -1;
    }
    
    return answer;
}
cs
#include <iostream>
#include <math.h>
using namespace std;
 
int max_mul;
 
int count_mul(int n)
{
    return log(n) / log(3);
}
 
int solve(int add, int mul, int n)
{
    int result = 0;
    
    // '*'의 최대 갯수보다 '+'의 갯수가 많아지면 종료
    if(2 * max_mul < add)
    {
        return 0;
    }
    
    if(n == 1)
    {
        return 1;
    }
    
    if(n % 3 == 0)
    {
        if(2 * (mul + 1<= add)
        {
            result += solve(add, mul + 1, n / 3);    
        }
  
        result += solve(add + 3, mul, n - 3);
    }
    else if(n % 3 != 0)
    {
        result += solve(add + (n % 3), mul, n - (n % 3));
    }
    
    return result;
}
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
int solution(int n) 
{
    int answer = 0;
    
    max_mul = count_mul(n);
    answer = solve(00, n);
    
    return answer;
}
cs

+ Recent posts