#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
bool cal(vector<int> stones, int mid, int k)
{
    int cnt = 0;
    
    for(int i = 0; i < stones.size(); i++)
    {
        if(stones[i] - mid <= 0)
        {
            cnt++;
        }
        else
        {
            cnt = 0;
        }
        
        if(cnt >= k)
        {
            return true;
        }
    }
    
    return false;
}
 
int solution(vector<int> stones, int k) 
{
    int answer = 0;
    int left = 1;
    int right = 200000000;
    int Max = 0;
    
    while(left <= right)
    {
        // 친구의 수
        int mid = (left + right) / 2;
        
        if(cal(stones, mid, k))
        {
            right = mid-1;
        }
        else
        {
            left = mid+1;
            
            // 연속된 (stone - 친구 == 0) 부분이 k개 미만인 최댓값 구하기
            if(Max < mid)
            {
                Max = mid;
            }
        }
    }
    
    // 연속된 (stone - 친구 == 0) 부분이 k개 이상이려면 최댓값+1
    return Max+1;
}
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 <string>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
 
char Map[110][110];
int row, col;
 
int dx[4= {-1100};
int dy[4= {00-11};
 
int safe(int x, int y)
{
    if(0 <= x && x < row && 0 <= y && y < col)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
bool solve(int x, int y)
{        
    char Find = Map[x][y];
    
    for(int i = 0; i < 4; i++)
    {
        int xpos = x + dx[i];
        int ypos = y + dy[i];
        
        while(safe(xpos, ypos) == 1 && (Map[xpos][ypos] == '.' || Map[xpos][ypos] == Find))
        {
            if(Map[xpos][ypos] == Find)
            {
                Map[x][y] = '.';
                Map[xpos][ypos] = '.';
                return true;
            }
            
            for(int j = 0; j < 4; j++)
            {
                if(i == j || (i == 0 && j == 1|| (i == 1 && j == 0|| (i == 2 && j == 3|| (i == 3 && j == 2))
                {
                    continue;
                }
 
                int nx = xpos + dx[j];
                int ny = ypos + dy[j];
               
                while(safe(nx, ny) == 1 && (Map[nx][ny] == '.' || Map[nx][ny] == Find))
                {
                    if(Map[nx][ny] == Find)
                    {
                        Map[x][y] = '.';
                        Map[nx][ny] = '.';
                        return true;
                    }
                    
                    nx += dx[j];
                    ny += dy[j];
                }
            }
 
            xpos += dx[i];
            ypos += dy[i];
        }   
    }
    
    return false;
}
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
string solution(int m, int n, vector<string> board) 
{
    string answer = "";
    row = m;
    col = n;
    
    vector<pair<charpair<intint>>> reserve;   
    map<charint> alphabet_check;  
    
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            Map[i][j] = board[i][j];
            
            if('A' <= Map[i][j] && Map[i][j] <= 'Z' && alphabet_check[Map[i][j]] == 0)
            {
                alphabet_check[Map[i][j]] = 1;
                reserve.push_back({Map[i][j], {i, j}});
            }
        }
    }
    sort(reserve.begin(), reserve.end());
    
    bool Flag = true;
    while(Flag)
    {
        Flag = false;
        
        for(int i = 0; i < reserve.size(); i++)
        {
            Flag = solve(reserve[i].second.first, reserve[i].second.second);
            
            if(Flag == true)
            {
                answer += reserve[i].first;
                reserve.erase(reserve.begin()+i);
                
                break;
            }
        }
    }
 
    if(reserve.size() != 0)
    {
        answer = "IMPOSSIBLE";
    }
    
    return answer;
}
cs
#include <string>
#include <iostream>
using namespace std;
 
int solution(string dirs) 
{
    int answer = 0;
    int visited[11][11][4= {0}; // 상, 하, 좌, 우
    int x = 5;
    int y = 5;
    
    for(int i = 0; i < dirs.size(); i++)
    {
        if(dirs[i] == 'U')
        {
            if(y+1 > 10)
            {
                continue;
            }
            
            if(visited[x][y][0== 0 && visited[x][y+1][1== 0)
            {
                visited[x][y][0= 1;
                visited[x][y+1][1= 1;
                answer++;
            }
            
            y++;
        }
        else if(dirs[i] == 'D')
        {
            if(y-1 < 0)
            {
                continue;
            }
            
            if(visited[x][y][1== 0 && visited[x][y-1][0== 0)
            {
                visited[x][y][1= 1;
                visited[x][y-1][0= 1;
                answer++;
            }
            
            y--;
        }
        else if(dirs[i] == 'L')
        {
            if(x-1 < 0)
            {
                continue;
            }
            
            if(visited[x][y][2== 0 && visited[x-1][y][3== 0)
            {
                visited[x][y][2= 1;
                visited[x-1][y][3= 1;
                answer++;
            }
            
            x--;
        }
        else if(dirs[i] == 'R')
        {
            if(x+1 > 10)
            {
                continue;
            }
            
            if(visited[x][y][3== 0 && visited[x+1][y][2== 0)
            {
                visited[x][y][3= 1;
                visited[x+1][y][2= 1;
                answer++;
            }
           
            x++;
        }
    }
    
   return answer;
}
cs
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
int solution(vector<int> A, vector<int> B) 
{
    int answer = 0;
    
    sort(A.begin(), A.end());  
    sort(B.begin(), B.end());
    
    for(int i = A.size()-1; i >= 0; i--)
    {
        int left = 0;
        int right = B.size()-1;
        
        while(left <= right)
        {
            int mid = (left + right) / 2;
            
            if(B[mid] > A[i])
            {
                B.erase(B.begin()+mid);
                answer++;
                
                break;
            }
            else if(B[mid] <= A[i])
            {
                left = mid+1;
            }
        }
    }    
    
    return answer;
}
cs
#include <string>
#include <vector>
using namespace std;
 
long long dp[2010];
 
long long solve(int n)
{
    if(n == 0)
    {
        return 1;
    }
    else if(n < 0)
    {
        return 0;
    }
    
    if(dp[n] != 0)
    {
        return dp[n];
    }
    
    dp[n] = (solve(n-1+ solve(n-2)) % 1234567;
 
    return dp[n] % 1234567;
}
 
long long solution(int n) 
{
    long long answer = solve(n);
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
 
long long solution(int n, vector<int> works) 
{
    long long answer = 0;
    
    priority_queue<int> pq;
    
    for(int i = 0; i < works.size(); i++)
    {
        pq.push(works[i]);
    }
    
    for(int i = 1; i <= n; i++)
    {
        int value = pq.top();
        pq.pop();
        
        if(value == 0)
        {
            break;
        }
        
        value--;
        pq.push(value);
    }
    
    for(int i = 0; i < works.size(); i++)
    {
        long long value = pq.top();
        pq.pop();
        
        answer += value * value;
    }
    
    return answer;
}
cs

+ Recent posts