diff --git a/README.md b/README.md index 7cdbfb4..83bcea6 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,4 @@ ### Algorithm Study -#### 특이사항 - * 5557 DP심화 이차원으루다가 - * 1937 DFS&DP 욕심쟁이판다 - * 2583 뭐가 문제지 ...? -> sort할때 vector.begin() vector.end() - **list sort하는 게 더 쉽구나 list.sort()하면 그냥 끝 !** - * 2206 벽부수고이동 아직 푸는중 - * 2198 블랙잭 시간초과 이유 : 브루트 포스할 때 3번 뽑은 이후에도 계속 뽑아서 체크하기 때문이었다. - * 1339 아직 c++ 문법에 익숙하지 않은 나,, comp함수 직접 만들어서 적용하기, 내림차순 정렬 방법, include 해야한다 - * 3055 탈출에는 두개의 BFS가 있다. 물번지기와 고슴도치 이동, 두 가지 BFS를 하나의 Queue에 넣으면 더 쉽다! 클래스에다가 type설정하여 type별로 다르게 행동하도록 설정한다. - * 풀어야 할 것 : 1753 (다익스트라사용) , 3860 할로윈 묘지 (벨만포드 사용) / 그래프 이론 공부 : 단절점 찾기 - * 4195 map 자료구조 사용하는 방법 - -#### 일기와 메모 - * 2020.07.25 Codeforces 가입하고 (제일 쉬워보이는) round 656에서 문제를 쇼쇼쇽 풀어보았다. - * 2020.08.10 - 2020.08.21 SDS알고특강 - * 9, 10월 목표 : 쉬운문제에 머물지 말기 어려운 문제 도전 https://www.acmicpc.net/workbook/view/4349 문제집 사용해서 다양한 알고리즘 적용해보기 + * https://solved.ac/profile/cbr5964 + * https://www.notion.so/575a370490a1459d955c0ef9b8f38974 diff --git a/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp b/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp new file mode 100644 index 0000000..83557e7 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int N; +bool board[1001][1001]; +int ans = 0; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +void check(int x, int y) +{ + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx <= 1000 && ny <= 1000) + { + if (board[nx][ny]) + { + int count = 0; + for (int j = 0; j < 4; j++) + { + int nnx = nx + dx[j]; + int nny = ny + dy[j]; + if (nnx >= 0 && nny >= 0 && nnx <= 1000 && nny <= 1000) + { + if (board[nnx][nny]) + count++; + } + } + if (!board[x][y] && count == 3) + ans -= 1; + else if (!board[x][y] && count == 2) + ans += 1; + } + } + } + + board[x][y] = true; +} + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + for (int i = 0; i < N; i++) + { + int x, y; + cin >> x >> y; + check(x, y); + cout << ans << "\n"; + } + + return 0; +} diff --git a/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp b/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp new file mode 100644 index 0000000..6119446 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +using namespace std; + +bool dot[3000][3000]; +int total_cow = 0; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +queue > q; + +void evaluate(int x, int y) +{ + if (!dot[x][y]) + return; + //cout << x << " " << y << "\n"; + int count = 0; + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (dot[nx][ny]) + count++; + } + + if (count == 3) + { + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (!dot[nx][ny]) + { + q.push({nx, ny}); + } + } + } +} + +void check_dot(int x, int y) +{ + q.push({x, y}); + while (!q.empty()) + { + x = q.front().first; + y = q.front().second; + q.pop(); + + if (dot[x][y]) + continue; + + total_cow++; + dot[x][y] = true; + evaluate(x, y); + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + evaluate(nx, ny); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int N, x, y; + cin >> N; + + for (int i = 1; i <= N; i++) + { + cin >> x >> y; + check_dot(x + 1000, y + 1000); + cout << total_cow - i << "\n"; + } + + return 0; +} diff --git a/USACO Bronze/2020-2021 Season/Year of the Cow.cpp b/USACO Bronze/2020-2021 Season/Year of the Cow.cpp new file mode 100644 index 0000000..35a4a30 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Year of the Cow.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +int get_year(string w) +{ + if (w == "Ox") + return 1; + if (w == "Tiger") + return 2; + if (w == "Rabbit") + return 3; + if (w == "Dragon") + return 4; + if (w == "Snake") + return 5; + if (w == "Horse") + return 6; + if (w == "Goat") + return 7; + if (w == "Monkey") + return 8; + if (w == "Rooster") + return 9; + if (w == "Dog") + return 10; + if (w == "Pig") + return 11; + return 12; +} + +int get_month(int n) +{ + return (n - 1) % 12 + 1; +} + +map input; + +int main() +{ + input["Bessie"] = 1; + + int N; + cin >> N; + string tmp[8]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < 8; j++) + { + cin >> tmp[j]; + } + + int diff = abs(input[tmp[7]] - get_year(tmp[0])); + if (tmp[3] == "previous") + { + int dx = get_month(input[tmp[7]]) - get_year(tmp[4]); + if (dx < 0) + dx += 12; + input[tmp[0]] = input[tmp[7]] - dx; + cout << dx << "\n"; + } + else + { + int dx = get_year(tmp[4]) - get_month(input[tmp[7]]); + if (dx > 0) + dx -= 12; + input[tmp[0]] = input[tmp[7]] + dx; + cout << dx << "\n"; + } + + cout << tmp[0] << " " << input[tmp[0]] << "\n"; + } + + cout << abs(input["Bessie"] - input["Elsie"]) << "\n"; + + return 0; +} diff --git a/baekjoon/14503.cpp b/baekjoon/14503.cpp new file mode 100644 index 0000000..ea06bcc --- /dev/null +++ b/baekjoon/14503.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int N, M; +int R, C, dir; +bool visited[50][50]; +bool wall[50][50]; + +int dx[8] = {0, 1, 0, -1, 0, 1, 0, -1}; +int dy[8] = {-1, 0, 1, 0, -1, 0, 1, 0}; +int bx[4] = {1, 0, -1, 0}; +int by[4] = {0, 1, 0, -1}; + +int dfs(int x, int y, int d) +{ + int res = 0; + + if (!visited[x][y]) + res += 1; + + visited[x][y] = true; + + cout << x << " " << y << " " << d << "\n"; + + for (int i = d; i < d + 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny] && !wall[nx][ny]) + { + res += dfs(nx, ny, (i + 1) % 4); + break; + } + } + } + + if (res == 1) + { + int nx = x + bx[d]; + int ny = y + by[d]; + cout << x << "/" << y << "/" << nx << "/" << ny << "\n"; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !wall[nx][ny]) + { + res += dfs(nx, ny, d); + } + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + cin >> R >> C >> dir; + + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + int t; + cin >> t; + if (t == 1) + wall[i][j] = true; + } + } + + cout << dfs(R, C, dir) << "\n"; + + return 0; +} diff --git a/baekjoon/DP/10422.cpp b/baekjoon/DP/10422.cpp new file mode 100644 index 0000000..47851b2 --- /dev/null +++ b/baekjoon/DP/10422.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int T, L; +long long D[5001]; + +int solve(int n){ + if(D[n]!=0 || n%2==1) return D[n]; + + for(int y = 2; y<= n; y+= 2){ + D[n] = (D[n] + (long long)solve(y-2)*solve(n-y)) % 1000000007; + } + return D[n]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + + int i, j; + + D[0]=1; + D[2]=1; + + for (i = 0; i < T; i++) + { + cin >> L; + cout << solve(L) << "\n"; + } + return 0; +} diff --git a/baekjoon/DP/11058.cpp b/baekjoon/DP/11058.cpp new file mode 100644 index 0000000..1b29f57 --- /dev/null +++ b/baekjoon/DP/11058.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long long D[105]; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + D[1]=1;D[2]=2;D[3]=3; + + for(int i=3; i<=N; i++){ + D[i] = D[i-1]+1; + for(int j=1; j<=i-3-1; j++){ + D[i] = max(D[i], D[i-3-j]*(2+j)); + } + } + + cout << D[N]<<"\n"; + + return 0; +} diff --git a/baekjoon/DP/1135.cpp b/baekjoon/DP/1135.cpp new file mode 100644 index 0000000..c9f80fa --- /dev/null +++ b/baekjoon/DP/1135.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +vector ch[51]; +bool visited[51]; +int N; + +int dfs(int n){ + if(ch[n].size()==0) return 0; + + int time[51]; + for(int i=0; i> N; + int x; + cin >> x; + for(int i=1; i> x; + ch[x].push_back(i); + } + + cout << dfs(0) << "\n"; + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/12969.cpp b/baekjoon/DP/12969.cpp new file mode 100644 index 0000000..550e9fd --- /dev/null +++ b/baekjoon/DP/12969.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int N, K; +// 30개중에 A,B가 몇개있는데 +bool DP[31][31][31][436]; +int A, B; +bool flag = false; +string ans = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + +bool solve(int n, int a, int b, int k){ + if(DP[n][a][b][k] == true) return false; + DP[n][a][b][k] = true; + + if(n==1){ + if(k==0){ + if(a+b==1){ + if(a==1) ans[n-1] = 'A'; + else ans[n-1] = 'B'; + } + else if(a+b==0){ + ans[n-1] = 'C'; + } + else return false; + return true; + } + return false; + } + + ans[n-1] = 'A'; + if(a-1>=0 && solve(n-1, a-1, b, k)) return true; + + ans[n-1] = 'B'; + if(k-a>=0 && b-1>=0 && solve(n-1, a, b-1, k-a)) return true; + + ans[n-1] = 'C'; + if(k-a-b>=0 && solve(n-1, a, b, k-a-b)) return true; + + return false; +} + +int main(){ + cin >> N >> K; + for(int a=0; a<=N && !flag; a++){ + for(int b=0; a+b<=N; b++){ + if(solve(N, a, b, K)){ + flag = true; + for(int c=0; c + +using namespace std; + +int N, L, R; +long long D[101][101][101]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> L >> R; + + D[1][1][1] = 1; + + for(int i=2; i<=N; i++){ + for(int j=1; j<=L; j++){ + for(int k=1; k<=R; k++){ + D[i][j][k] += D[i-1][j-1][k] + D[i-1][j][k-1] + D[i-1][j][k]*(i-2) % 1000000007; + D[i][j][k] %= 1000000007; + } + } + } + + cout << D[N][L][R] << "\n"; + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/14239.cpp b/baekjoon/DP/14239.cpp new file mode 100644 index 0000000..1d42afe --- /dev/null +++ b/baekjoon/DP/14239.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int Acount = 0, Bcount = 0, Ccount = 0; +char ans[51]; +bool DP[51][51][51][3][3]; + +bool solve(int a, int b, int c, int pre, int pre2) +{ + if (a == Acount && b == Bcount && c == Ccount) + return true; + + if (DP[a][b][c][pre][pre2]) + return false; + DP[a][b][c][pre][pre2] = true; + + if (a < Acount) + { + ans[a + b + c] = 'A'; + if (solve(a + 1, b, c, 0, pre)) + return true; + } + if (b < Bcount && pre != 1) + { + ans[a + b + c] = 'B'; + if (solve(a, b + 1, c, 1, pre)) + return true; + } + if (c < Ccount && pre != 2 && pre2 != 2) + { + ans[a + b + c] = 'C'; + if (solve(a, b, c + 1, 2, pre)) + return true; + } + return false; +} + +int main() +{ + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == 'A') + Acount++; + else if (s[i] == 'B') + Bcount++; + else + Ccount++; + } + + if (solve(0, 0, 0, -1, -1)) + { + for (int i = 0; i < s.length(); i++) + { + cout << ans[i]; + } + } + else + { + cout << "-1\n"; + } + return 0; +} diff --git a/baekjoon/DP/14925.cpp b/baekjoon/DP/14925.cpp new file mode 100644 index 0000000..952209e --- /dev/null +++ b/baekjoon/DP/14925.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int N, M; +int board[1000][1000]; +int D[1000][1000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for(int i=0; i> board[i][j]; + if(board[i][j]==0) D[i][j] = 1; + } + } + + for(int i=1; i + +using namespace std; +int DP[2500][2500]; +int DP2[2501]; + +int main(){ + string str; + cin >> str; + + for(int i=0; i=0; i--){ + if(str[i] == str[j] && DP[i+1][j-1] > 0) DP[i][j] = DP[i+1][j-1]+2; + } + } + + // for(int i=0; i0) DP2[j+1] = min(DP2[j+1], DP2[i] + 1); + } + } + + cout << DP2[str.length()] << "\n"; + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/1520.cpp b/baekjoon/DP/1520.cpp new file mode 100644 index 0000000..8cb4643 --- /dev/null +++ b/baekjoon/DP/1520.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +int N, M; +int board[500][500]; +bool visited[500][500]; +int dp[500][500]; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int dfs(int i, int j) +{ + int res = dp[i][j]; + visited[i][j] = true; + + for (int x = 0; x < 4; x++) + { + int tx = i + dx[x]; + int ty = j + dy[x]; + if (tx >= 0 && ty >= 0 && tx < N && ty < M && board[tx][ty] < board[i][j]) + { + if (!visited[tx][ty]) + res += dfs(tx, ty); + else + res += dp[tx][ty]; + } + } + + dp[i][j] = res; + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> board[i][j]; + } + } + + dp[N - 1][M - 1] = 1; + cout << dfs(0, 0) << "\n"; + + return 0; +} diff --git a/baekjoon/DP/1562.cpp b/baekjoon/DP/1562.cpp new file mode 100644 index 0000000..3dfab6b --- /dev/null +++ b/baekjoon/DP/1562.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int N; +int DP[101][10][1024]; + +int main(){ + + cin >> N; + for(int i=1; i<10; i++){ + DP[1][i][1<0) + DP[i][j][nk] += DP[i-1][j-1][k]; + DP[i][j][nk] %= 1000000000; + if(j<9) + DP[i][j][nk] += DP[i-1][j+1][k]; + DP[i][j][nk] %= 1000000000; + } + } + } + + int ans = 0; + for(int i=0; i<10; i++){ + ans += DP[N][i][1023]; + ans %= 1000000000; + } + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/1563.cpp b/baekjoon/DP/1563.cpp new file mode 100644 index 0000000..4abf4f1 --- /dev/null +++ b/baekjoon/DP/1563.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int N; +bool visited[1001][5][5]; +int D[1001][5][5]; + +// 출석 0 지각 1 결석 2 + +int solve(int n, int l, int a){ + if(visited[n][l][a]) return D[n][l][a]; + + visited[n][l][a] = true; + + if(n == 1){ + if(l+a >= 2) return D[1][l][a] = 0; + return D[1][l][a] = 1; + } + + int res = 0; + + if(a>0){ + res += solve(n-1, l, a-1); + }else{ + for(int i=0;i<=2; i++){ + res += solve(n-1, l, i); + } + if(l>0){ + for(int i=0;i<=2;i++){ + res += solve(n-1, l-1, i); + } + } + } + + return D[n][l][a] = res % 1000000; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + cout << (solve(N, 0, 0) + solve(N, 1, 0) + solve(N, 0, 1) + solve(N, 0, 2) + solve(N, 1, 1) + solve(N,1,2))%1000000<< "\n"; + + return 0; +} diff --git a/baekjoon/DP/2225.cpp b/baekjoon/DP/2225.cpp new file mode 100644 index 0000000..8e23af9 --- /dev/null +++ b/baekjoon/DP/2225.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int N, K; +long long D[201][201]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + int i, j, k; + for (i = 1; i <= K; i++) + { + D[0][i] = 1; + } + for (i = 1; i <= N; i++) + { + D[i][1] = 1; + } + + for (i = 1; i <= N; i++) + { + for (j = 2; j <= K; j++) + { + D[i][j] = (D[i - 1][j] + D[i][j - 1]) % 1000000000; + } + } + + cout << D[N][K] << "\n"; + return 0; +} diff --git a/baekjoon/DP/2228.cpp b/baekjoon/DP/2228.cpp new file mode 100644 index 0000000..8aaf77e --- /dev/null +++ b/baekjoon/DP/2228.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int N, M; + +int DP[101][51]; +int A[101]; + +int solve(int n, int M){ + + if(M==0) return 0; + if(n<=0) return -3276800; + if(DP[n][M] != 0) return DP[n][M]; + + DP[n][M] = solve(n-1, M); + int sum = 0; + for(int i=n; i>0; i--){ + sum += A[i]; + DP[n][M] = max(DP[n][M], solve(i-2, M-1) + sum); + } + return DP[n][M]; +} + +int main(){ + + cin >> N >> M; + + for(int i=1; i<=N; i++){ + cin >> A[i]; + } + + cout << solve(N, M); + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/2602.cpp b/baekjoon/DP/2602.cpp new file mode 100644 index 0000000..d3d6814 --- /dev/null +++ b/baekjoon/DP/2602.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +string target; +string dari[2]; +int DP[2][101][21]; + +long long solve(int row, int col, int idx){ + + //cout << row << col << idx <<"\n"; + + if(idx == target.length()-1){ + return 1; + } + + if(DP[row][col][idx] != -1) return DP[row][col][idx]; + + int nrow = 1 - row; + + long long res = 0; + for(int j=col+1; j> target >> dari[0] >> dari[1]; + + for(int i=0;i<2;i++){ + for(int j=0;j + +using namespace std; + +int N; +int A[50000]; +int D[50001][4]; +int S[50001]; +int T; + +int main(){ + cin >> N; + for(int i=0; i> A[i]; + cin >> T; + + S[1] = A[0]; + for(int i=2; i<=N; i++) S[i] = S[i-1]+A[i-1]; + + for(int i=1; i<=N; i++){ + for(int j=1; j<=3; j++){ + D[i][j] = D[i-1][j]; + if(i-T>=0) D[i][j] = max(D[i][j], D[i-T][j-1] + S[i]-S[i-T]); + cout << D[i][j] << " "; + } + cout << "\n"; + } + + cout << D[N][3] << "\n"; + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/2624.cpp b/baekjoon/DP/2624.cpp new file mode 100644 index 0000000..c6d0dba --- /dev/null +++ b/baekjoon/DP/2624.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int A[100][2]; +long long D[100][10001]; + +int main() +{ + int T, N; + cin >> T >> N; + + for(int i=0;i> A[i][0] >> A[i][1]; + } + + for(int k=0; k<=A[0][1]; k++){ + if(A[0][0]*k > T) break; + D[0][A[0][0]*k] = 1; + } + + for(int i=1;i T) break; + D[i][j+A[i][0]*k] += D[i-1][j]; + } + } + } + + cout << D[N-1][T] << "\n"; + + return 0; +} diff --git a/baekjoon/DP/2629.cpp b/baekjoon/DP/2629.cpp new file mode 100644 index 0000000..1abfce0 --- /dev/null +++ b/baekjoon/DP/2629.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int N, M; +int C[30]; +int T[7]; +int D[30][15001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> C[i]; + D[i][0] = 1; + } + + D[0][C[0]]=1; + + for(int i=1; i= 0) D[i][j] = max(D[i][j], D[i-1][j-C[i]]); + } + } + + cin >> M; + for(int i=0; i> T[i]; + for(int j=0; j+T[i] <= 15000; j++){ + if(D[N-1][j] == 1 && D[N-1][j+T[i]] == 1){ + flag = true; + break; + } + } + if(flag) cout << "Y "; + else cout << "N "; + } + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/2631.cpp b/baekjoon/DP/2631.cpp new file mode 100644 index 0000000..ff4aa8f --- /dev/null +++ b/baekjoon/DP/2631.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +int N; +int A[200]; +vector lis; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + + lis.push_back(-1); + + for (i = 0; i < N; i++) + { + cin >> A[i]; + + if (lis.back() <= A[i]) + { + lis.push_back(A[i]); + } + else + { + vector::iterator it = lower_bound(lis.begin(), lis.end(), A[i]); + *it = A[i]; + } + } + + cout << N - (lis.size() - 1) << "\n"; + return 0; +} diff --git a/baekjoon/DP/2688.cpp b/baekjoon/DP/2688.cpp new file mode 100644 index 0000000..59465b7 --- /dev/null +++ b/baekjoon/DP/2688.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +long long D[65][10]; +int T, N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + + for (int t = 0; t < T; t++) + { + cin >> N; + + for (int i = 0; i <= N; i++) + { + for (int j = 0; j < 10; j++) + { + D[i][j] = 0; + } + } + for (int j = 0; j < 10; j++) + { + D[1][j] = 1; + } + + for (int i = 2; i <= N; i++) + { + for (int j = 0; j < 10; j++) + { + for (int k = 0; k <= j; k++) + { + D[i][j] += D[i - 1][k]; + } + } + } + + long long ans = 0; + for (int j = 0; j < 10; j++) + { + ans += D[N][j]; + } + cout << ans << "\n"; + } + return 0; +} diff --git a/baekjoon/DP/2698.cpp b/baekjoon/DP/2698.cpp new file mode 100644 index 0000000..2bf6230 --- /dev/null +++ b/baekjoon/DP/2698.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +int T, N, K; +int D[101][101][2]; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + int t; + + cin >> T; + int i, j; + for (t = 0; t < T; t++) + { + cin >> N >> K; + for (i = 0; i <= N; i++) + { + for (j = 0; j <= K; j++) + { + D[i][j][0] = 0; + D[i][j][1] = 0; + } + } + + D[1][0][0] = 1; + D[1][0][1] = 1; + + for (i = 2; i <= N; i++) + { + D[i][0][1] = D[i - 1][0][0]; + D[i][0][0] = D[i - 1][0][0] + D[i - 1][0][1]; + } + + for (i = 2; i <= N; i++) + { + for (j = 1; j <= K; j++) + { + D[i][j][0] = D[i - 1][j][0] + D[i - 1][j][1]; + D[i][j][1] = D[i - 1][j - 1][1] + D[i - 1][j][0]; + } + } + + cout << D[N][K][0] + D[N][K][1] << "\n"; + } + + return 0; +} diff --git a/baekjoon/DP/2758.cpp b/baekjoon/DP/2758.cpp new file mode 100644 index 0000000..504791f --- /dev/null +++ b/baekjoon/DP/2758.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int T, N, M; +long long D[15][2001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int i=0;i> N >> M; + for(int x=1; x<=M; x++) D[1][x] = 1; + for(int x=2; x<=N; x++){ + for(int y=1; y<=M; y++){ + D[x][y] = 0; + for(int k=1;k<=y/2;k++){ + D[x][y] += D[x-1][k]; + } + } + } + long long ans = 0; + for(int y=1; y<=M; y++){ + ans += D[N][y]; + } + cout << ans << "\n"; + } + + return 0; +} diff --git a/baekjoon/DP/2879.cpp b/baekjoon/DP/2879.cpp new file mode 100644 index 0000000..3d07aa0 --- /dev/null +++ b/baekjoon/DP/2879.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int N; +int A[1000]; + +int main(){ + int x; + cin >> N; + for(int i=0; i> A[i]; + } + + for(int i=0; i> x; + A[i] = -A[i]+x; + //cout << A[i] << "\t"; + } + + int ans = 0; + + int prev = A[0]; + for(int i=1; i=0){ + if(A[i]<0){ + ans += abs(prev); + } + else if(prev < A[i]){ + ; + } + else{ + ans += abs(prev-A[i]); + } + prev = A[i]; + }else{ + if(A[i]>=0){ + ans += abs(prev); + } + else if(prev > A[i]){ + } + else{ + ans += abs(prev-A[i]); + } + prev = A[i]; + } + } + ans += abs(prev); + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/4811.cpp b/baekjoon/DP/4811.cpp new file mode 100644 index 0000000..75491f1 --- /dev/null +++ b/baekjoon/DP/4811.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int T, N; +long long D[31][31]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + while (true) + { + + cin >> N; + + if (N == 0) + break; + + for (int i = 0; i <= N; i++) + { + for (int j = 0; j <= N; j++) + { + D[i][j] = 0; + } + } + + D[0][N] = 1; + + for (int j = N - 1; j >= 0; j--) + { + for (int i = N; i >= 0; i--) + { + if (i - 1 >= 0) + { + D[i][j] += D[i - 1][j + 1]; + } + if (i + 1 <= N) + { + D[i][j] += D[i + 1][j]; + } + } + } + + cout << D[0][0] << "\n"; + } + + return 0; +} diff --git a/baekjoon/DP/7570.cpp b/baekjoon/DP/7570.cpp new file mode 100644 index 0000000..99956d2 --- /dev/null +++ b/baekjoon/DP/7570.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int N; +int D[1000001]; +int ans = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i, x; + for (i = 1; i <= N; i++) + { + cin >> x; + D[x] = D[x - 1] + 1; + ans = max(ans, D[x]); + } + cout << N - ans << "\n"; + + return 0; +} diff --git a/baekjoon/DP/8895.cpp b/baekjoon/DP/8895.cpp new file mode 100644 index 0000000..512aa9a --- /dev/null +++ b/baekjoon/DP/8895.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long long DP[21][21][21]; +int T, N, L, R; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + DP[1][1][1] = 1; + cin >> T; + for(int t=0; t> N >> L >> R; + for(int i=2; i<=N; i++){ + for(int j=1; j<=L; j++){ + for(int k=1; k<=R; k++){ + DP[i][j][k] = DP[i-1][j-1][k] + DP[i-1][j][k-1] + (i-2)*DP[i-1][j][k]; + } + } + } + cout << DP[N][L][R] << "\n"; + } + + return 0; +} \ No newline at end of file diff --git a/baekjoon/DP/9084.cpp b/baekjoon/DP/9084.cpp new file mode 100644 index 0000000..3bde789 --- /dev/null +++ b/baekjoon/DP/9084.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int T, N; +int won[20]; +int M; +long long DP[10001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0; t> N; + for(int i=0; i> won[i]; + } + cin >> M; + + for(int j=0; j<=M; j++) DP[j] = 0; + DP[0] = 1; + + for(int i=0; i +#include + +using namespace std; + +string target; +string files[100]; +int N; +vector ans; +int DP[100][105][105]; + +bool solve(int n, int i, int j){ + bool res = false; + if(DP[n][i][j]!=-1) return DP[n][i][j]; + if(i==files[n].length() && j==target.length()) return DP[n][i][j] = true; + if(i>files[n].length()) return DP[n][i][j] = false; + if(j>=target.length()) return DP[n][i][j] = false; + + if(target[j] == '*'){ + res = res || solve(n, i, j+1); + if(i<=files[n].length()) + res = res || solve(n, i+1, j); + if(i<=files[n].length()) + res = res || solve(n, i+1, j+1); + } + else if(i> target >> N; + + for(int i=0; i> files[i]; + if(solve(i, 0, 0)){ + ans.push_back(files[i]); + } + } + + for(string s:ans){ + cout << s << "\n"; + } + + return 0; +} diff --git a/baekjoon/KMP/16916.cpp b/baekjoon/KMP/16916.cpp new file mode 100644 index 0000000..c682645 --- /dev/null +++ b/baekjoon/KMP/16916.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int main() +{ + + string s, p; + cin >> s >> p; + + vector pi(p.length(), 0); + int j = 0; + for (int i = 1; i < p.length(); i++) + { + while (j > 0 && p[i] != p[j]) + { + j = pi[j - 1]; + } + + if (p[i] == p[j]) + { + pi[i] = ++j; + } + } + + j = 0; + for (int i = 0; i < s.length(); i++) + { + + while (j > 0 && s[i] != p[j]) + { + j = pi[j - 1]; + } + + if (s[i] == p[j]) + { + j++; + } + + if (j == p.length()) + { + cout << "1\n"; + return 0; + } + } + + cout << "0\n"; + + return 0; +} diff --git a/baekjoon/KMP/1701.cpp b/baekjoon/KMP/1701.cpp new file mode 100644 index 0000000..c201052 --- /dev/null +++ b/baekjoon/KMP/1701.cpp @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string s; + cin >> s; + + vector pi(s.length(), 0); + + int ans = 0; + + for (int index = 0; index < s.length() - 1; index++) + { + fill(pi.begin(), pi.end(), 0); + int j = 0; + for (int i = index + 1; i < s.length(); i++) + { + while (j > 0 && s[i] != s[j + index]) + { + j = pi[j - 1 + index]; + } + if (s[i] == s[j + index]) + { + pi[i] = ++j; + } + ans = max(ans, pi[i]); + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/12904.cpp" "b/baekjoon/\352\265\254\355\230\204/12904.cpp" new file mode 100644 index 0000000..93b7579 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/12904.cpp" @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +int main() +{ + + string s, target; + cin >> s >> target; + + while (s != target && target.length() > 0) + { + int index = target.length() - 1; + if (target[index] == 'A') + { + target = target.substr(0, index); + } + else + { + target = target.substr(0, index); + reverse(target.begin(), target.end()); + } + cout << target << "\t"; + } + + if (s == target) + { + cout << "1\n"; + } + else + { + cout << "0\n"; + } + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/13459.cpp" "b/baekjoon/\352\265\254\355\230\204/13459.cpp" new file mode 100644 index 0000000..3b785ca --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/13459.cpp" @@ -0,0 +1,154 @@ +#include +#include + +using namespace std; + +#define RED 0 +#define BLUE 1 + +struct node +{ + int x, y, move; +}; + +int N, M; +string board[10]; +queue bfs[2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {-1, 1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + if (board[i][j] == 'R') + { + bfs[RED].push({i, j, 0}); + board[i][j] = '.'; + } + else if (board[i][j] == 'B') + { + bfs[BLUE].push({i, j, 0}); + board[i][j] = '.'; + } + } + } + + while (!bfs[RED].empty()) + { + int rx = bfs[RED].front().x; + int ry = bfs[RED].front().y; + int bx = bfs[BLUE].front().x; + int by = bfs[BLUE].front().y; + int move = bfs[RED].front().move; + + if (bx == -1) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + if (board[rx][ry] == 'O') + { + cout << "1\n"; + return 0; + } + + if (move >= 10) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + for (int dir = 0; dir < 4; dir++) + { + int idx = 0; + if (dir == 0) + { + if (ry > by) + idx = 1; + } + else if (dir == 1) + { + if (by > ry) + idx = 1; + } + else if (dir == 2) + { + if (bx > rx) + idx = 1; + } + else + { + if (bx < rx) + { + idx = 1; + } + } + + for (int t = 0; t < 2; t++) + { + int x = bfs[idx].front().x; + int y = bfs[idx].front().y; + + while (true) + { + int nx = x + dx[dir]; + int ny = y + dy[dir]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == 'O') + { + if (idx == RED) + { + x = nx; + y = ny; + break; + } + else + { + x = -1; + y = -1; + break; + } + } + else if (board[nx][ny] == '#') + break; + else + { + if (t == 1 && bfs[1 - idx].back().x == nx && bfs[1 - idx].back().y == ny) + { + break; + } + else + { + x = nx; + y = ny; + } + } + } + } + + bfs[idx].push({x, y, move + 1}); + idx = 1 - idx; + } + } + + bfs[RED].pop(); + bfs[BLUE].pop(); + } + + cout << "0\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/13490.cpp" "b/baekjoon/\352\265\254\355\230\204/13490.cpp" new file mode 100644 index 0000000..94312dd --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/13490.cpp" @@ -0,0 +1,154 @@ +#include +#include + +using namespace std; + +#define RED 0 +#define BLUE 1 + +struct node +{ + int x, y, move; +}; + +int N, M; +string board[10]; +queue bfs[2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {-1, 1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + if (board[i][j] == 'R') + { + bfs[RED].push({i, j, 0}); + board[i][j] = '.'; + } + else if (board[i][j] == 'B') + { + bfs[BLUE].push({i, j, 0}); + board[i][j] = '.'; + } + } + } + + while (!bfs[RED].empty()) + { + int rx = bfs[RED].front().x; + int ry = bfs[RED].front().y; + int bx = bfs[BLUE].front().x; + int by = bfs[BLUE].front().y; + int move = bfs[RED].front().move; + + if (bx == -1) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + if (board[rx][ry] == 'O') + { + cout << move << "\n"; + return 0; + } + + if (move >= 10) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + for (int dir = 0; dir < 4; dir++) + { + int idx = 0; + if (dir == 0) + { + if (ry > by) + idx = 1; + } + else if (dir == 1) + { + if (by > ry) + idx = 1; + } + else if (dir == 2) + { + if (bx > rx) + idx = 1; + } + else + { + if (bx < rx) + { + idx = 1; + } + } + + for (int t = 0; t < 2; t++) + { + int x = bfs[idx].front().x; + int y = bfs[idx].front().y; + + while (true) + { + int nx = x + dx[dir]; + int ny = y + dy[dir]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == 'O') + { + if (idx == RED) + { + x = nx; + y = ny; + break; + } + else + { + x = -1; + y = -1; + break; + } + } + else if (board[nx][ny] == '#') + break; + else + { + if (t == 1 && bfs[1 - idx].back().x == nx && bfs[1 - idx].back().y == ny) + { + break; + } + else + { + x = nx; + y = ny; + } + } + } + } + + bfs[idx].push({x, y, move + 1}); + idx = 1 - idx; + } + } + + bfs[RED].pop(); + bfs[BLUE].pop(); + } + + cout << "-1\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/14503.cpp" "b/baekjoon/\352\265\254\355\230\204/14503.cpp" new file mode 100644 index 0000000..1d0197a --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/14503.cpp" @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int N, M; +int R, C, dir; +bool visited[50][50]; +bool wall[50][50]; + +// 0 1 2 3 +// 위 왼 아 오 +// 위 동 아 왼 +int dx[8] = {0, 1, 0, -1, 0, 1, 0, -1}; +int dy[8] = {-1, 0, 1, 0, -1, 0, 1, 0}; +int bx[4] = {1, 0, -1, 0}; +int by[4] = {0, 1, 0, -1}; + +int dfs(int x, int y, int d) +{ + int res = 0; + + if (!visited[x][y]) + { + res += 1; + visited[x][y] = true; + } + + //cout << x << " " << y << " " << d << "\n"; + + bool flag = false; + + for (int i = d; i < d + 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny] && !wall[nx][ny]) + { + res += dfs(nx, ny, (i + 1) % 4); + flag = true; + break; + } + } + } + + if (!flag) + { + int nx = x + bx[d]; + int ny = y + by[d]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !wall[nx][ny]) + { + res += dfs(nx, ny, d); + } + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + cin >> R >> C >> dir; + + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + + if (dir == 1) + { + dir = 3; + } + else if (dir == 3) + { + dir = 1; + } + + cout << dfs(R, C, dir) << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/14719.cpp" "b/baekjoon/\352\265\254\355\230\204/14719.cpp" new file mode 100644 index 0000000..cc6a129 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/14719.cpp" @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int N, M; +int A[500]; +bool rain[500][500]; +int ans; + +void check(int i, int j) +{ + bool left = false, right = false; + + for (int y = j - 1; y >= 0; y--) + { + if (A[y] - 1 >= i) + { + left = true; + break; + } + } + for (int y = j + 1; y < M; y++) + { + if (A[y] - 1 >= i) + { + right = true; + break; + } + } + + if (left && right) + { + rain[i][j] = true; + ans++; + cout << i << " " << j << " " << ans << "\n"; + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < M; i++) + { + cin >> A[i]; + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (A[j] > i) + continue; + + if (i == 0 || rain[i - 1][j] || A[j] == i) + { + check(i, j); + } + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/16236.cpp" "b/baekjoon/\352\265\254\355\230\204/16236.cpp" new file mode 100644 index 0000000..c92f267 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/16236.cpp" @@ -0,0 +1,124 @@ +#include +#include + +using namespace std; + +int A[20][20]; +int N; +int sx, sy; +int ans = 0; +int ssize = 2; +int scount = 0; + +struct node +{ + int x, y, move; + + bool operator<(const node &a) const + { + if (move == a.move) + { + if (x == a.x) + { + return y > a.y; + } + return x > a.x; + } + return move > a.move; + } +}; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +void solve() +{ + while (true) + { + queue search; + priority_queue find; + bool visited[400]; + fill(visited, visited + 400, 0); + + search.push({sx, sy, 0}); + visited[sx * N + sy] = true; + while (!search.empty()) + { + int x = search.front().x; + int y = search.front().y; + int move = search.front().move; + search.pop(); + + if (A[x][y] > 0 && A[x][y] < ssize) + { + find.push({x, y, move}); + continue; + } + + if (!find.empty() && find.top().move < move + 1) + { + continue; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx * N + ny] && A[nx][ny] <= ssize) + { + search.push({nx, ny, move + 1}); + visited[nx * N + ny] = true; + } + } + } + + if (!find.empty()) + { + sx = find.top().x; + sy = find.top().y; + scount++; + if (scount == ssize) + { + ssize++; + scount = 0; + } + + A[sx][sy] = 0; + ans += find.top().move; + cout << sx << " " << sy << " " << ans << "\n"; + } + else + { + break; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + cin >> A[i][j]; + if (A[i][j] == 9) + { + sx = i; + sy = j; + A[i][j] = 0; + } + } + } + + int x; + + solve(); + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/16935.cpp" "b/baekjoon/\352\265\254\355\230\204/16935.cpp" new file mode 100644 index 0000000..e1718d2 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/16935.cpp" @@ -0,0 +1,155 @@ +#include + +using namespace std; +int N, M, R; +int A[100][100]; + +void turn(int n) +{ + int temp[100][100]; + if (n == 1) + { + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + temp[N - 1 - j][i] = A[j][i]; + } + } + } + else if (n == 2) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[i][M - 1 - j] = A[i][j]; + } + } + } + else if (n == 3) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[j][N - i - 1] = A[i][j]; + } + } + swap(N, M); + } + else if (n == 4) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[M - 1 - j][i] = A[i][j]; + } + } + swap(N, M); + } + else if (n == 5) + { + for (int i = 0; i < N / 2; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i + N / 2][j]; + } + } + for (int i = 0; i < N / 2; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i][j - M / 2]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i][j + M / 2]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i - N / 2][j]; + } + } + } + else + { + for (int i = 0; i < N / 2; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i][j + M / 2]; + } + } + for (int i = 0; i < N / 2; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i + N / 2][j]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i - N / 2][j]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i][j - M / 2]; + } + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + A[i][j] = temp[i][j]; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> R; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> A[i][j]; + } + } + + for (int i = 0; i < R; i++) + { + int x; + cin >> x; + turn(x); + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cout << A[i][j] << " "; + } + cout << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/17143.cpp" "b/baekjoon/\352\265\254\355\230\204/17143.cpp" new file mode 100644 index 0000000..f794c36 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/17143.cpp" @@ -0,0 +1,143 @@ +#include +#include + +using namespace std; + +int dx[4] = {-1, 1, 0, 0}; +int dy[4] = {0, 0, 1, -1}; +int bd[4] = {1, 0, 3, 2}; + +struct shark +{ + int x, y, speed, dir, size; + bool live; +}; + +int N, M, S; +int ans = 0; + +void catch_shark(int here, vector &myshark) +{ + + int x = 200; + int index = -1; + + for (int i = 0; i < myshark.size(); i++) + { + if (myshark[i].live && here == myshark[i].y && myshark[i].x < x) + { + index = i; + x = myshark[i].x; + } + } + + if (index != -1) + { + ans += myshark[index].size; + myshark[index].live = false; + } +} + +void shark_move(vector &myshark) +{ + int temp[100][100]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[i][j] = -1; + } + } + + for (int i = 0; i < myshark.size(); i++) + { + if (!myshark[i].live) + continue; + + int x = myshark[i].x; + int y = myshark[i].y; + int dir = myshark[i].dir; + int speed = myshark[i].speed; + + int nx = x + dx[dir] * speed; + int ny = y + dy[dir] * speed; + + while (nx < 0 || nx >= N) + { + dir = bd[dir]; + if (nx < 0) + nx *= -1; + else + nx = 2 * N - nx - 2; + } + while (ny < 0 || ny >= M) + { + dir = bd[dir]; + if (ny < 0) + ny *= -1; + else + ny = 2 * M - ny - 2; + } + + myshark[i].dir = dir; + myshark[i].x = nx; + myshark[i].y = ny; + + //cout << "!" << i << " " << myshark[i].x << " " << myshark[i].y << " " << myshark[i].dir << "\n"; + + if (temp[nx][ny] == -1) + { + temp[nx][ny] = i; + } + else + { + int there = temp[nx][ny]; + if (!myshark[there].live) + { + temp[nx][ny] = i; + } + else if (myshark[there].size < myshark[i].size) + { + myshark[there].live = false; + temp[nx][ny] = i; + } + else + { + myshark[i].live = false; + } + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> S; + + vector myshark; + + for (int i = 0; i < S; i++) + { + int r, c, s, d, z; + cin >> r >> c >> s >> d >> z; + myshark.push_back({r - 1, c - 1, s, d - 1, z, true}); + } + + for (int move = 0; move < M; move++) + { + catch_shark(move, myshark); + shark_move(myshark); + + for (int i = 0; i < myshark.size(); i++) + { + //cout << myshark[i].x << " " << myshark[i].y << " " << myshark[i].dir << " " << myshark[i].size << " " << myshark[i].speed << "\n"; + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/17406.cpp" "b/baekjoon/\352\265\254\355\230\204/17406.cpp" new file mode 100644 index 0000000..9f4d462 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/17406.cpp" @@ -0,0 +1,92 @@ +#include + +using namespace std; + +int N, M, K; +int A[100][100]; + +int[][] spin(int[][] A, int x, int y, int z) +{ + int temp[100][100]; + for (int k = z; k >= 0; k--) + { + for (int j = y - k + 1; j <= y + k; j++) + { + temp[x - k][j] = A[x - k][j - 1]; + } + + for (int i = x - k + 1; i <= x + k; i++) + { + temp[i][y + k] = A[i - 1][y + k]; + } + + for (int j = y - k; j < y + k; j++) + { + temp[x + k][j] = A[x + k][j + 1]; + } + + for (int i = x - k; i < x + k; i++) + { + temp[i][y - k] = A[i + 1][y - k]; + } + } + + temp[x][y] = A[x][y]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (i < x - z || i > x + z || j < y - z || j > y + z) + { + temp[i][j] = A[i][j]; + } + } + } + return temp; +} + +int get_sum(int[][] temp) +{ + + int res = 200000; + + for (int i = 0; i < N; i++) + { + int sum = 0; + for (int j = 0; j < M; j++) + { + sum += temp[i][j]; + } + res = min(res, sum); + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> K; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> A[i][j]; + } + } + + int x, y, z; + int ans = 2000000; + + for (int i = 0; i < K; i++) + { + cin >> x >> y >> z; + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/1756.cpp" "b/baekjoon/\352\265\254\355\230\204/1756.cpp" new file mode 100644 index 0000000..41a40e7 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/1756.cpp" @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int D, N; +int A[300000]; +int B[300000]; +int M[300000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> D >> N; + + for(int i=0; i> A[i]; + } + + for(int j=0; j> B[j]; + } + + M[0] = A[0]; + + for(int i=1; i=0; i--){ + if(now= B[now]){ + now++; + last = i; + } + } + + if(now==N) cout << last+1 << "\n"; + else cout << "0\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\265\254\355\230\204/1990.cpp" "b/baekjoon/\352\265\254\355\230\204/1990.cpp" new file mode 100644 index 0000000..fde6c4a --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/1990.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +using namespace std; + +int A, B; +vector pelindrome; + +void solve(int n, string str){ + if(n>0 && n<=8){ + if(str[0] == '0'){ + ; + } + else if(A <= atoi(str.c_str()) && atoi(str.c_str()) <= B){ + pelindrome.push_back(atoi(str.c_str())); + } + } + else if(n>8){ + return; + } + + if(n==0){ + for(int i=0; i<=9; i++){ + solve(n+1, str + char(i+'0')); + } + } + for(int i=0; i<=9; i++){ + solve(n+2, char(i+'0') + str + char(i+'0')); + } +} + +int main(){ + ios::sync_with_stdio(0); + cout.tie(0); + cin >> A >> B; + + solve(0, ""); + + sort(pelindrome.begin(), pelindrome.end()); + for(int p:pelindrome){ + bool prime = true; + for(int i=2; i<=floor(sqrt(p)); i++){ + if(p%i==0){ + prime = false; + break; + } + } + if(prime) cout << p << "\n"; + } + + cout << "-1\n"; + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\265\254\355\230\204/5624.cpp" "b/baekjoon/\352\265\254\355\230\204/5624.cpp" new file mode 100644 index 0000000..1f0ac77 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/5624.cpp" @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define OFFSET 200000 +bool exist[400010]; +int A[5000]; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int x; + int ans = 0; + + for (int i = 0; i < N; i++) + { + cin >> A[i]; + + for (int j = 0; j < i; j++) + { + if (exist[A[i] - A[j] + OFFSET]) + { + ans++; + break; + } + } + + for (int j = 0; j <= i; j++) + { + exist[A[j] + A[i] + OFFSET] = true; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/7490.cpp" "b/baekjoon/\352\265\254\355\230\204/7490.cpp" new file mode 100644 index 0000000..7224864 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/7490.cpp" @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +int T, N; + +void solve(int n, string s) +{ + if (n == N) + { + int a = 0, pre = 0; + char c = '+'; + for (int i = 0; i < s.length(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + { + pre = pre * 10 + s[i] - '0'; + } + else if (s[i] != ' ') + { + if (c == '+') + a = a + pre; + else + a = a - pre; + + c = s[i]; + pre = 0; + } + } + if (c == '+') + a = a + pre; + else + a = a - pre; + if (a == 0) + cout << s << "\n"; + + return; + } + solve(n + 1, s + " " + to_string(n + 1)); + solve(n + 1, s + "+" + to_string(n + 1)); + solve(n + 1, s + "-" + to_string(n + 1)); +} + +int main() +{ + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N; + solve(1, "1"); + cout << "\n"; + } + return 0; +} diff --git "a/baekjoon/\352\265\254\355\230\204/9328.cpp" "b/baekjoon/\352\265\254\355\230\204/9328.cpp" new file mode 100644 index 0000000..8493d24 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/9328.cpp" @@ -0,0 +1,139 @@ +#include +#include + +using namespace std; + +int T, N, M; +string board[100]; +bool visited[100][100][27]; +bool key[26]; + +int count_key() +{ + int res = 0; + for (int i = 0; i < 26; i++) + { + if (key[i]) + res++; + } + return res; +} + +void check_boundary(queue, int> > &q) +{ + int ihave = count_key(); + for (int i = 0; i < N; i++) + { + if (board[i][0] != '*' && !visited[i][0][ihave]) + { + q.push(make_pair(make_pair(i, 0), ihave)); + visited[i][0][ihave] = true; + } + if (board[i][M - 1] != '*' && !visited[i][M - 1][ihave]) + { + q.push(make_pair(make_pair(i, M - 1), ihave)); + visited[i][M - 1][ihave] = true; + } + } + + for (int i = 1; i < M - 1; i++) + { + if (board[0][i] != '*' && !visited[0][i][ihave]) + { + q.push(make_pair(make_pair(0, i), ihave)); + visited[0][i][ihave] = true; + } + if (board[N - 1][i] != '*' && !visited[N - 1][i][ihave]) + { + q.push(make_pair(make_pair(N - 1, i), ihave)); + visited[N - 1][i][ihave] = true; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + for (int k = 0; k < 27; k++) + { + visited[i][j][k] = false; + } + } + } + for (int k = 0; k < 26; k++) + { + key[k] = false; + } + + string k; + cin >> k; + if (k[0] != '0') + { + for (int i = 0; i < k.length(); i++) + { + key[k[i] - 'a'] = true; + } + } + + queue, int> > q; + check_boundary(q); + int ans = 0; + + while (!q.empty()) + { + int x = q.front().first.first; + int y = q.front().first.second; + int k = q.front().second; + q.pop(); + + if (board[x][y] == '$') + { + ans++; + board[x][y] = '.'; + } + if (board[x][y] >= 'a' && board[x][y] <= 'z' && !key[board[x][y] - 'a']) + { + key[board[x][y] - 'a'] = true; + } + if (board[x][y] >= 'A' && board[x][y] <= 'Z' && !key[board[x][y] - 'A']) + continue; + + int dx[4] = {0, 0, 1, -1}; + int dy[4] = {1, -1, 0, 0}; + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M && board[nx][ny] != '*') + { + if (!visited[nx][ny][count_key()]) + { + q.push(make_pair(make_pair(nx, ny), count_key())); + visited[nx][ny][count_key()] = true; + } + } + } + check_boundary(q); + } + + cout << ans << "\n"; + } + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" new file mode 100644 index 0000000..058f95a --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define INF 10001 + +int N, M; +int d[101][101]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + int i, j, x, y; + + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + for (i = 0; i < M; i++) + { + cin >> x >> y; + d[x][y] = 1; + } + + for (x = 1; x <= N; x++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (d[i][j] > d[i][x] + d[x][j]) + { + d[i][j] = d[i][x] + d[x][j]; + } + } + } + } + + for (i = 1; i <= N; i++) + { + int count = 0; + for (j = 1; j <= N; j++) + { + if (d[i][j] == INF && d[j][i] == INF) + { + count++; + } + } + cout << count << "\n"; + } + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" new file mode 100644 index 0000000..80cde86 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +int N, M; +string board[50]; + +struct node{ + int x, y, n, k; +}; + +queue q; +bool visited[50][50]; +int DP[50][50]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; + +int dfs(int x, int y){ + if(DP[x][y]!=0) return DP[x][y]; + int res = 0; + int k = board[x][y] - '0'; + visited[x][y] = true; + for(int i=0; i<4; i++){ + int nx = x+dx[i]*k; + int ny = y+dy[i]*k; + if(nx<0||ny<0||nx>=N||ny>=M||board[nx][ny]=='H') continue; + if(visited[nx][ny]) return DP[x][y] = 1000000; + res = max(res, dfs(nx, ny)); + } + visited[x][y] = false; + return DP[x][y] = res + 1; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> board[i]; + } + + dfs(0,0); + if(DP[0][0] >= 1000000) cout << "-1\n"; + else cout << DP[0][0] << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" new file mode 100644 index 0000000..d9503f2 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int N; +int graph[20][20]; +bool exclude[20][20]; +int result; + +void floyd() +{ + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + + if (i == j || j == k || i == k) + { + continue; + } + else if (graph[i][j] > graph[i][k] + graph[k][j]) + { + result = -1; + return; + } + else if (graph[i][j] == graph[i][k] + graph[k][j]) + { + exclude[i][j] = true; + } + } +} + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + cin >> graph[i][j]; + + floyd(); + + if (result != -1) + { + + for (int i = 0; i < N; i++) + + for (int j = 0; j < N; j++) + + if (!exclude[i][j]) + result += graph[i][j]; + + cout << result / 2 << "\n"; + } + else + { + cout << -1 << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" new file mode 100644 index 0000000..db1734c --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define INF 1600000 + +int N, K, S; +int d[401][401]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N >> K; + + int i, j, x, y; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + for (i = 0; i < K; i++) + { + cin >> x >> y; + d[x][y] = 1; + } + + for (x = 1; x <= N; x++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (d[i][j] > d[i][x] + d[x][j]) + { + d[i][j] = d[i][x] + d[x][j]; + } + } + } + } + + cin >> S; + + for (i = 0; i < S; i++) + { + cin >> x >> y; + if (d[x][y] < INF) + { + cout << "-1\n"; + } + else if (d[y][x] < INF) + { + cout << "1\n"; + } + else + { + cout << "0\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" new file mode 100644 index 0000000..6eec575 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +vector edges[3001]; +int N; +bool visited[3001]; +bool cycle[3001]; + +void init() +{ + for (int i = 1; i <= N; i++) + { + visited[i] = false; + } +} + +void check(int n, int start, int d) +{ + visited[start] = true; + for (int v : edges[start]) + { + if (v == n && d > 1) + { + cycle[n] = true; + return; + } + if (!visited[v]) + { + check(n, v, d + 1); + } + } +} + +int solve(int n, int r) +{ + visited[n] = true; + if (cycle[n]) + return 0; + + r = 100000000; + for (int v : edges[n]) + { + if (!visited[v]) + r = min(r, solve(v, r) + 1); + } + return r; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + int x, y; + for (int i = 0; i < N; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + for (int i = 1; i <= N; i++) + { + init(); + check(i, i, 0); + } + + for (int i = 1; i <= N; i++) + { + init(); + cout << solve(i, 0) << " "; + } + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" new file mode 100644 index 0000000..3ce5fbe --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" @@ -0,0 +1,108 @@ +#include +#include + +using namespace std; + +int dist[100][100]; +int board[100][100]; +bool visited[100][100]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int N; + +struct bfs +{ + int x, y, c, turn; + + bool operator<(const bfs &a) const + { + return turn > a.turn; + } +}; + +priority_queue bfsQueue; + +void dfs(int i, int j, int n) +{ + visited[i][j] = true; + board[i][j] = n; + bfsQueue.push({i, j, n, 0}); + + for (int x = 0; x < 4; x++) + { + int nx = i + dx[x]; + int ny = j + dy[x]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny] && board[nx][ny] != 0) + { + dfs(nx, ny, n); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int n = 1, i, j; + + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + cin >> board[i][j]; + } + } + + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (!visited[i][j] && board[i][j] != 0) + { + dfs(i, j, n); + n++; + } + } + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.top().x; + int y = bfsQueue.top().y; + int turn = bfsQueue.top().turn; + int c = bfsQueue.top().c; + bfsQueue.pop(); + + if (c == -1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < N) + { + if (board[nx][ny] == 0 && !visited[nx][ny]) + { + board[nx][ny] = c; + dist[nx][ny] = turn + 1; + bfsQueue.push({nx, ny, c, turn + 1}); + visited[nx][ny] = true; + } + else if (board[nx][ny] != 0 && board[nx][ny] != c) + { + bfsQueue.push({nx, ny, -1, turn + dist[nx][ny]}); + } + } + } + } + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" new file mode 100644 index 0000000..4b5d8e1 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" @@ -0,0 +1,87 @@ +#include +#include + +using namespace std; + +#define INF 200001 + +int N, M, K; +int F[201][201]; +vector friends; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + F[i][j] = INF; + } + F[i][i] = 0; + } + + + for(int i=0; i> x >> y >> z; + F[x][y] = z; + } + + cin >> K; + for(int i=0; i> x; + friends.push_back(x); + } + + for(int k=1; k<=N; k++){ + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + if(F[i][j] > F[i][k] + F[k][j]){ + F[i][j] = F[i][k] + F[k][j]; + } + } + } + } + + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + cout << F[i][j] << "\t"; + } + cout << "\n"; + } + + vector ans; + int minsum = INF * 400; + + for(int i=1; i<=N; i++){ + bool flag = true; + int tmp = 0; + for(int j:friends){ + if(F[i][j] == INF || F[j][i] == INF){ + flag = false; + break; + } + tmp = max(tmp, F[i][j] + F[j][i]); + } + + if(!flag) continue; + + if(minsum > tmp){ + ans.clear(); + ans.push_back(i); + minsum = tmp; + } + else if(minsum == tmp){ + ans.push_back(i); + } + } + + for(int a:ans){ + cout << a << " "; + } + cout << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" new file mode 100644 index 0000000..ef0d451 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" @@ -0,0 +1,85 @@ +#include +#include + +using namespace std; + +#define INF 2500 + +int N; +int d[51][51]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for (x = 1; x <= N; x++) + { + for (y = 1; y <= N; y++) + { + d[x][y] = INF; + } + d[x][x] = 0; + } + + while (true) + { + cin >> x >> y; + if (x == -1 && y == -1) + { + break; + } + + d[x][y] = 1; + d[y][x] = 1; + } + + for (int k = 1; k <= N; k++) + { + for (x = 1; x <= N; x++) + { + for (y = 1; y <= N; y++) + { + if (d[x][y] > d[x][k] + d[k][y]) + { + d[x][y] = d[x][k] + d[k][y]; + } + } + } + } + + int minscore = INF; + vector ans; + + for (x = 1; x <= N; x++) + { + int nowmax = 0; + for (y = 1; y <= N; y++) + { + nowmax = max(d[x][y], nowmax); + } + if (nowmax < minscore) + { + minscore = nowmax; + ans.clear(); + ans.push_back(x); + } + else if (nowmax == minscore) + { + ans.push_back(x); + } + } + + cout << minscore << " " << ans.size() << "\n"; + for (int a : ans) + { + cout << a << " "; + } + cout << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" new file mode 100644 index 0000000..0da0582 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" @@ -0,0 +1,44 @@ +N = int(input()) +digit = [(i, 0) for i in range(36)] + +for i in range(N): + number = input() + for n in range(len(number)-1, -1, -1): + tmp = number[n] + if ord(tmp) >= ord('0') and ord(tmp) <= ord('9'): + tmp = int(tmp) + else: + tmp = ord(tmp) - ord('A') + 10 + + digit[tmp] = (tmp, digit[tmp][1] + pow(36, len(number)-1-n)) + +K = int(input()) + +check = [] +for i in range(36): + check.append((digit[i][1]*(35-i), i)) + +check.sort(reverse=True) + +for i in range(K): + digit[check[i][1]] = (35, digit[check[i][1]][1]) + +sum35 = 0 +for i in range(36): + sum35 += digit[i][0] * digit[i][1] + +def to36(n): + if n < 36: + if n >= 0 and n <= 9: + print(n, end='') + else: + print(chr(ord('A')+n-10), end='') + return + + to36(n//36) + if n%36 >= 0 and n%36 <= 9: + print(n%36, end='') + else: + print(chr(ord('A')+n%36-10), end='') + +to36(sum35) \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" new file mode 100644 index 0000000..772d6e0 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; + +int main() { + int m, n; + int num; + int cnt = 0; + vector v1, v2; + + cin >> m; + for (int i = 0; i < m; i++) { + cin >> num; + v1.push_back(num); + } + + cin >> n; + for (int i = 0; i < n; i++) { + cin >> num; + v2.push_back(num); + } + + sort(v1.begin(), v1.end(), greater()); + sort(v2.begin(), v2.end(), greater()); + + if (v2[0] > v1[0]) { + cout << -1 << "\n"; + } + else { + while (!v2.empty()) { + int index = 0; + for (int i = 0; i < v2.size(); i++) { + if (index == v1.size()) { + break; + } + if (v1[index] >= v2[i]) { + index++; + v2.erase(v2.begin() + i); + i = i - 1; + } + } + cnt++; + } + cout << cnt << "\n"; + } +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" new file mode 100644 index 0000000..7f1e158 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; + +int N; + +typedef struct Node +{ + int start, end; + + inline bool operator<(const Node &a) const + { + if (a.end < end) + return true; + return false; + } +} Node; + +vector > classes; +priority_queue pq; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int ans = 0, i, x, y; + cin >> N; + for (i = 0; i < N; i++) + { + cin >> x >> y; + classes.push_back({x, y}); + } + + sort(classes.begin(), classes.end()); + + for (i = 0; i < N; i++) + { + if (pq.empty()) + { + pq.push({classes[i].first, classes[i].second}); + ans++; + } + else if (pq.top().end <= classes[i].first) + { + Node c = pq.top(); + pq.pop(); + c.end = classes[i].second; + pq.push(c); + } + else + { + Node c; + c.start = classes[i].first; + c.end = classes[i].second; + pq.push(c); + ans++; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" new file mode 100644 index 0000000..1f9fd7f --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int N, K; +int A[300000]; +int diff[300000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + + for(int i=0; i> A[i]; + } + + for(int i=0; i +#include + +using namespace std; + +struct node +{ + int due; + int score; + + bool operator<(const node &a) const + { + if (a.due == due) + return score < a.score; + return due > a.due; + } +}; + +int N; +priority_queue pq; +priority_queue doit; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for (int i = 0; i < N; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int time = 1; + int ans = 0; + while (!pq.empty()) + { + int d = pq.top().due; + int s = pq.top().score; + pq.pop(); + + //cout << d << " " << s << " " << ans << "\n"; + + if (d >= time) + { + ans += s; + doit.push(-s); + time++; + } + else if (!doit.empty() && -doit.top() < s) + { + ans += doit.top(); + doit.pop(); + doit.push(-s); + ans += s; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" new file mode 100644 index 0000000..5337cbc --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; +int N, M; +vector positive; +vector negative; +int answer = 0; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int x; + for(int i=0; i> x; + if(x<0) negative.push_back(x); + else positive.push_back(x); + } + + sort(negative.begin(), negative.end()); + sort(positive.begin(), positive.end()); + + int ps = 0, ns = 0; + int pn = positive.size()-1; + int nn = negative.size()-1; + + // ns nn ps pn + if(negative.empty() || (!positive.empty() && positive[pn] > -negative[0])){ + answer += positive[pn]; + pn -= M; + } + else{ + answer -= negative[ns]; + ns += M; + } + + while(ns <= nn){ + answer -= negative[ns]*2; + ns += M; + } + + while(ps <= pn){ + answer += positive[pn]*2; + pn -= M; + } + + cout << answer << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" new file mode 100644 index 0000000..856bd69 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int N; +int x = -1000000000, y = -1000000000; +int ans = 0; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i, j; + for(int k=0; k> i >> j; + + if(i <= y && j <= y) continue; + if(i <= y){ + y=j; + } + else{ + ans += y-x; + x = i; + y = j; + } + } + + ans += y-x; + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" new file mode 100644 index 0000000..54c553d --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +int N, K; +vector elec; +int use[101]; +int uselength = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + int i, j, x, u; + int ans = 0; + for (i = 0; i < K; i++) + { + cin >> x; + elec.push_back(x); + } + + for (i = 0; i < K; i++) + { + int now = elec[i]; + + bool flag = false; + for (u = 0; u < uselength; u++) + { + if (use[u] == now) + { + flag = true; + break; + } + } + if (flag) + continue; + + if (uselength < N) + { + use[uselength] = now; + uselength++; + continue; + } + + int next = 0; + int lastIndex = -1; + + for (u = 0; u < uselength; u++) + { + flag = false; + for (j = i + 1; j < K; j++) + { + if (use[u] == elec[j]) + { + flag = true; + if (lastIndex < j) + { + lastIndex = j; + next = u; + } + break; + } + } + if (!flag) + { + next = u; + break; + } + } + + use[next] = now; + ans++; + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" new file mode 100644 index 0000000..bb3b068 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; + +int N; +vector A; +vector B; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int i, x; + long long ans = 0; + cin >> N; + for (i = 0; i < N; i++) + { + cin >> x; + if (x > 0) + A.push_back(x); + else if (x <= 0) + B.push_back(x); + } + sort(A.begin(), A.end()); + sort(B.begin(), B.end()); + + for (i = A.size() - 1; i >= 0; i--) + { + if (i > 0 && ((long long)A[i] * A[i - 1]) > ((long long)A[i] + A[i - 1])) + { + ans += (long long)A[i] * A[i - 1]; + i--; + } + else + { + ans += A[i]; + } + } + for (i = 0; i < B.size(); i++) + { + if (i + 1 < B.size()) + { + ans += (long long)B[i] * B[i + 1]; + i++; + } + else + { + ans += B[i]; + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" new file mode 100644 index 0000000..8e42c32 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; +int N; + +struct Work +{ + long long deadline, ramen; + + bool operator<(const Work &a) const + { + if (ramen == a.ramen) + { + return deadline > a.deadline; + } + return ramen > a.ramen; + } +}; + +struct PushedWork +{ + long long deadline, ramen; + + bool operator<(const PushedWork &a) const + { + if (deadline == a.deadline) + { + return ramen < a.ramen; + } + return deadline > a.deadline; + } +}; + +priority_queue pq; +priority_queue pq2; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + long long x, y; + long long ans = 0; + for (i = 0; i < N; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int workdeadline = 1; + for (i = 1; i <= N; i++) + { + long long deadline = pq.top().deadline; + long long ramen = pq.top().ramen; + pq.pop(); + + //cout << i << " " << deadline << " " << ramen << "\n"; + + if (deadline >= workdeadline) + { + pq2.push({deadline, ramen}); + ans += ramen; + workdeadline++; + } + else if (!pq2.empty() && pq2.top().ramen < ramen) + { + pq2.push({deadline, ramen}); + ans += ramen - pq2.top().ramen; + pq2.pop(); + } + //cout << ans << "\n"; + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" new file mode 100644 index 0000000..cc8c9dc --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +int N; +long long endtime = 0, ans = 0; + +struct Node +{ + long long start, end; + + inline bool operator<(const Node &a) const + { + if (end == a.end) + return start < a.start; + return end < a.end; + } +}; + +vector meetings; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + long long x, y; + for (i = 0; i < N; i++) + { + cin >> x >> y; + meetings.push_back({x, y}); + } + + sort(meetings.begin(), meetings.end()); + + for (i = 0; i < N; i++) + { + if (meetings[i].start >= endtime) + { + ans++; + endtime = meetings[i].end; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" new file mode 100644 index 0000000..7f80a7e --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int N, maxv; +long long total; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int i = 0; i < N; i++) { + int a; + cin >> a; + total += a; + maxv = max(maxv, a); + } + if (total - maxv > maxv) { + cout << (total & 1); + } else { + cout << maxv - (total - maxv); + } + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" new file mode 100644 index 0000000..c2d8d1e --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" @@ -0,0 +1,58 @@ +#include +#include + +using namespace std; + +struct node{ + int p, d; + + bool operator<(const struct node& a) const{ + if(p == a.p){ + return d pq; +bool used[10001]; +int parent[10001]; + +int getp(int n){ + if(parent[n] == n) return n; + return parent[n] = getp(parent[n]); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int x, y; + for(int i=0; i> x >> y; + pq.push({x,y}); + } + for(int i=1; i<=10000; i++) parent[i] = i; + + int ans = 0; + + while(!pq.empty()){ + int d = pq.top().d; + int p = pq.top().p; + pq.pop(); + + parent[d] = getp(d); + if(parent[d]!=0){ + used[parent[d]] = true; + ans+=p; + //cout << p << " " << parent[d] << "\n"; + parent[parent[d]] = getp(parent[d]-1); + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" new file mode 100644 index 0000000..aa7154f --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +int N; +priority_queue< pair > pq; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + for(int i=0; i> x >> y; + pq.push({-x, -y}); + } + + int ans = 0; + + int left = -1000000000; + int right = -1000000000; + + while(!pq.empty()){ + int x = -pq.top().first; + int y = -pq.top().second; + pq.pop(); + + if(y<=right) continue; + if(x<=right){ + right = y; + } + else{ + ans += right-left; + left = x; + right = y; + } + } + ans += right-left; + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" new file mode 100644 index 0000000..0a0248a --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int N; +int A[256]; +int winner[256][256]; +int DP[256][256]; + +int solve(int start, int end){ + + if(start == end) { + return 0; + } + if(start == end-1){ + return DP[start][end] = abs(A[start]-A[end]); + } + if(DP[start][end]!=0) return DP[start][end]; + + int result = 999999999; + for(int i=start; i> N; + for(int i=0; i> A[i]; + + for(int i=0; i + +using namespace std; + +bool used[50001]; +int check[50001]; +int A[50000]; +int N; + +int main(){ + cin >> N; + + for(int i=0; i> A[i]; + } + + for(int i=1; i<=N; i++){ + check[i] = i; + } + + for(int i=0; i +#include + +using namespace std; + +int N; +int A[1000]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + + sort(A, A + N); + + int sum = 0; + for (i = 0; i < N; i++) + { + if (sum + 1 < A[i]) + { + cout << sum + 1 << "\n"; + return 0; + } + sum += A[i]; + } + + cout << sum + 1 << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" new file mode 100644 index 0000000..7cf6cc7 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" @@ -0,0 +1,44 @@ +#include +#include + +using namespace std; + +int N, K; +string s; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K >> s; + int i; + for (i = 0; i < N; i++) + { + if (stack.empty() || stack.back() >= s[i] - '0') + { + stack.push_back(s[i] - '0'); + } + else if (K > 0) + { + stack.pop_back(); + K--; + i--; + } + else + { + stack.push_back(s[i] - '0'); + } + } + while (K > 0) + { + stack.pop_back(); + K--; + } + for (int s : stack) + { + cout << s; + } + cout << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" new file mode 100644 index 0000000..05857d3 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int N, M; +string board[10000]; +bool visited[10000][500]; +int dir[3][2] = {{-1, 1}, {0, 1}, {1, 1}}; + +bool dfs(int i, int j) +{ + visited[i][j] = true; + if (j == M - 1) + return true; + + for (int d = 0; d < 3; d++) + { + int nx = i + dir[d][0]; + int ny = j + dir[d][1]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && board[nx][ny] == '.' && !visited[nx][ny]) + { + if (dfs(nx, ny)) + return true; + } + } + return false; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + } + int ans = 0; + for (i = 0; i < N; i++) + { + if (board[i][0] == '.' && !visited[i][0]) + { + if (dfs(i, 0)) + ans++; + } + } + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" new file mode 100644 index 0000000..7ac04a6 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; +class Delivery { +public: + int from; + int to; + int cnt; + Delivery(int from, int to, int cnt) { + this->from = from; + this->to = to; + this->cnt = cnt; + } +}; + +bool cmp(Delivery a, Delivery b) { + if (a.to < b.to) return true; + else return false; +} + +int main(void) { + int n, c, m; + cin >> n >> c >> m; + vector list; + vector left(n + 1, c); + int box_cnt = 0; + for (int i = 0; i < m; i++) { + int from, to, cnt; + cin >> from >> to >> cnt; + list.push_back(Delivery(from, to, cnt)); + } + sort(list.begin(), list.end(), cmp); + + for (auto d : list) { + + int min = left[d.from]; + for (int i = d.from + 1; i < d.to; i++) { + if (min > left[i]) min = left[i]; + } + + int cnt = d.cnt; + if (min < cnt) cnt = min; + + box_cnt += cnt; + + for (int i = d.from; i < d.to; i++) { + left[i] -= cnt; + } + } + cout << box_cnt; + return 0; +} diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" new file mode 100644 index 0000000..e9ff2f1 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" @@ -0,0 +1,66 @@ +#include +#include + +using namespace std; + +typedef struct node +{ + int start, end; + + bool operator<(const node &a) const + { + if (end == a.end) + { + return start > a.start; + } + + return end > a.end; + } +} node; + +int T, N, M; +bool check[1001]; + +int main() +{ + ios::sync_with_stdio(0); + cin >> T; + + int x, y; + for (int t = 0; t < T; t++) + { + priority_queue pq; + cin >> N >> M; + for (int i = 0; i <= N; i++) + { + check[i] = false; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int answer = 0; + while (!pq.empty()) + { + x = pq.top().start; + y = pq.top().end; + pq.pop(); + + for (int i = x; i <= y; i++) + { + if (!check[i]) + { + check[i] = true; + answer++; + break; + } + } + } + cout << answer << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" new file mode 100644 index 0000000..47a23e7 --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; +int N; +string b[100]; +bool v[2][100][100]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +#define f for(i=0;i=N||ny>=N||v[n][nx][ny]) continue; + if(b[i][j]==b[nx][ny]||(n==1&&(b[i][j]=='R'||b[i][j]=='G')&&(b[nx][ny]=='R'||b[nx][ny]=='G')))dfs(nx,ny,n); + } +} +int main(){ + cin>>N; + int i,j,k,c[2]={0,0}; + f cin>>b[i];} + f for(j=0;j + +using namespace std; + +string board[12]; +bool visited[12][6]; +int check[12][6]; + +void initvisit() +{ + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + visited[i][j] = false; + check[i][j] = 0; + } + } +} + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int dfs(int i, int j, int n) +{ + visited[i][j] = true; + check[i][j] = n; + + int res = 1; + + for (int d = 0; d < 4; d++) + { + int nx = i + dx[d]; + int ny = j + dy[d]; + if (nx >= 0 && ny >= 0 && nx < 12 && ny < 6 && !visited[nx][ny] && board[i][j] == board[nx][ny]) + { + res += dfs(nx, ny, n); + } + } + + return res; +} + +void gravity() +{ + for (int i = 11; i >= 0; i--) + { + for (int j = 0; j < 6; j++) + { + if (board[i][j] == '.') + { + for (int k = i - 1; k >= 0; k--) + { + if (board[k][j] != '.') + { + board[i][j] = board[k][j]; + board[k][j] = '.'; + break; + } + } + } + } + } +} + +int ans = 0; + +void boom(int n) +{ + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + if (check[i][j] == n) + { + board[i][j] = '.'; + visited[i][j] = false; + check[i][j] = 0; + } + } + } +} + +int main() +{ + for (int i = 0; i < 12; i++) + { + cin >> board[i]; + } + + int n; + bool flag; + + do + { + flag = false; + n = 1; + initvisit(); + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + if (board[i][j] != '.' && !visited[i][j]) + { + int a = dfs(i, j, n); + if (a > 3) + { + boom(n); + flag = true; + } + n++; + } + } + } + if (flag) + ans++; + gravity(); + } while (flag == true); + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" new file mode 100644 index 0000000..15959bb --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int N, A, B; +vector< pair > edges[100001]; +bool visited[100001]; +bool flag = false; +long long total = 0; +long long maxCost = 0; + +bool dfs(int n){ + + visited[n] = true; + if(n==B) return true; + + for(pair edge: edges[n]){ + int next = edge.second; + int cost = edge.first; + if(!visited[next]){ + if(dfs(next)){ + total += cost; + if(maxCost < cost) maxCost = cost; + return true; + } + } + } + return false; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> A >> B; + + for(int i=0; i> x >> y >> z; + edges[x].push_back(make_pair(z, y)); + edges[y].push_back(make_pair(z, x)); + } + + dfs(A); + + cout << total - maxCost << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" new file mode 100644 index 0000000..b2ae9dd --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; + +map graph[100001]; +bool visited[100001]; +int N; +queue check; +bool flag = true; + +void dfs(int n){ + + //cout << n << "/"; + + check.pop(); + visited[n]=true; + + while(!check.empty()){ + if(graph[n].find(check.front())!=graph[n].end() && !visited[check.front()]){ + dfs(check.front()); + } + else{ + return; + } + } +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for(int i=0; i> x >> y; + graph[x][y] = true; + graph[y][x] = true; + } + + for(int i=0; i> x; + check.push(x); + } + + if(check.front()==1) + dfs(check.front()); + + if(check.empty()) cout << "1\n"; + else cout << "0\n"; + + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" new file mode 100644 index 0000000..a441e0b --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; +int N; +int A[101]; +bool visited[101]; +bool done[101]; +vector ans; + +void dfs(int n) +{ + visited[n] = true; + int next = A[n]; + if (!visited[next]) + dfs(next); + + else if (!done[next]) + { + for (int i = next; i != n; i = A[i]) + ans.push_back(i); + ans.push_back(n); + } + done[n] = true; +} + +int main() +{ + cin >> N; + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + dfs(i); + } + + cout << ans.size() << "\n"; + sort(ans.begin(), ans.end()); + for (int a : ans) + { + cout << a << "\n"; + } + return 0; +} diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" new file mode 100644 index 0000000..bdd188b --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; +bool visited[501]; +int N, M; +vector adj[501]; + +bool dfs(int n, int pre) +{ + visited[n] = true; + + for (int next : adj[n]) + { + if (pre == next) + continue; + + if (visited[next]) + return false; + if (dfs(next, n) == false) + return false; + } + + return true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t = 0; + while (true) + { + t++; + int tree = 0; + cin >> N >> M; + if (N == 0 && M == 0) + break; + + for (int i = 1; i <= N; i++) + { + adj[i].clear(); + visited[i] = false; + } + + int x, y; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + adj[x].push_back(y); + adj[y].push_back(x); + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + { + if (dfs(i, 0)) + tree++; + } + } + cout << "Case " << t << ": "; + if (tree == 0) + cout << "No trees.\n"; + else if (tree == 1) + cout << "There is one tree.\n"; + else + cout << "A forest of " << tree << " trees.\n"; + } + return 0; +} diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" new file mode 100644 index 0000000..2601226 --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int T, N; +int p[100001]; +bool visited[100001]; +bool done[100001]; +int ans; + +void dfs(int n) +{ + visited[n] = true; + + int next = p[n]; + if (!visited[next]) + { + dfs(next); + } + else if (!done[next]) + { + for (int i = next; i != n; i = p[i]) + { + ans++; + } + ans++; + } + done[n] = true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + for (int t = 0; t < T; t++) + { + ans = 0; + cin >> N; + for (int i = 1; i <= N; i++) + { + cin >> p[i]; + visited[i] = false; + done[i] = false; + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + dfs(i); + } + cout << N - ans << "\n"; + } + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" new file mode 100644 index 0000000..5122102 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +struct Move +{ + int x, y, k, turn; +}; + +string board[1000]; +bool visited[1000][1000][10]; +int N, M, K; +queue bfsQueue; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M >> K; + int i, j; + for (i = 0; i < N; i++) + cin >> board[i]; + + if (board[0][0] == '0') + { + bfsQueue.push({0, 0, 0, 1}); + visited[0][0][0] = true; + } + else if (K > 0) + { + bfsQueue.push({0, 0, 1, 1}); + visited[0][0][1] = true; + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int k = bfsQueue.front().k; + int turn = bfsQueue.front().turn; + bfsQueue.pop(); + + if (x == N - 1 && y == M - 1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == '0' && !visited[nx][ny][k]) + { + bfsQueue.push({nx, ny, k, turn + 1}); + visited[nx][ny][k] = true; + } + else if (board[nx][ny] == '1' && k < K && !visited[nx][ny][k + 1]) + { + bfsQueue.push({nx, ny, k + 1, turn + 1}); + visited[nx][ny][k + 1] = true; + } + } + } + } + + cout << "-1\n"; + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" new file mode 100644 index 0000000..2c2a3d2 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include + +using namespace std; +int A, B, C, D; + +struct node +{ + int a, b, d; +}; + +set visited[100010]; +queue bfsQueue; + +bool isVisited(int x, int y) +{ + if (visited[x].find(y) == visited[x].end()) + return false; + return true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> A >> B >> C >> D; + bfsQueue.push({0, 0, 0}); + visited[0].insert(0); + + while (!bfsQueue.empty()) + { + int a = bfsQueue.front().a; + int b = bfsQueue.front().b; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (a == C && b == D) + { + cout << d << "\n"; + return 0; + } + + if (a != A && !isVisited(A, b)) + { + bfsQueue.push({A, b, d + 1}); + visited[A].insert(b); + } + if (b != B && !isVisited(a, B)) + { + bfsQueue.push({a, B, d + 1}); + visited[a].insert(B); + } + if (a != 0 && !isVisited(0, b)) + { + bfsQueue.push({0, b, d + 1}); + visited[0].insert(b); + } + if (b != 0 && !isVisited(a, 0)) + { + bfsQueue.push({a, 0, d + 1}); + visited[a].insert(0); + } + int x = min(A, a + b); + int y = max((a + b - A), 0); + if (x != a && y != b && !isVisited(x, y)) + { + bfsQueue.push({x, y, d + 1}); + visited[x].insert(y); + } + x = min(B, a + b); + y = max((a + b - B), 0); + if (x != b && y != a && !isVisited(y, x)) + { + bfsQueue.push({y, x, d + 1}); + visited[y].insert(x); + } + } + + cout << "-1\n"; + + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" new file mode 100644 index 0000000..f315328 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +struct node +{ + int x, y, z; + bool broken; +}; + +int N, M, sx, sy, ex, ey; +bool wall[1000][1000]; +bool visited[1000][1000][2]; +queue bfsQueue; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> sx >> sy >> ex >> ey; + sx--; + sy--; + ex--; + ey--; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + + bfsQueue.push({sx, sy, 0, false}); + visited[sx][sy][0] = true; + + int dx[4] = {0, 0, 1, -1}; + int dy[4] = {1, -1, 0, 0}; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int z = bfsQueue.front().z; + bool broken = bfsQueue.front().broken; + bfsQueue.pop(); + + if (ex == x && ey == y) + { + cout << z << "\n"; + return 0; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!wall[nx][ny] && !visited[nx][ny][broken]) + { + bfsQueue.push({nx, ny, z + 1, broken}); + visited[nx][ny][broken] = true; + } + else if (wall[nx][ny] && !visited[nx][ny][true] && !broken) + { + bfsQueue.push({nx, ny, z + 1, true}); + visited[nx][ny][true] = true; + } + } + } + } + + cout << "-1\n"; + + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" new file mode 100644 index 0000000..efc774f --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +map visited; +queue< pair > q; +int dx[9][4] = { + {1, 3, -1, -1}, + {0, 2, 4, -1}, + {1, 5, -1, -1}, + {0, 4, 6, -1}, + {3, 5, 1, 7}, + {4, 2, 8, -1}, + {3, 7, -1, -1}, + {6, 4, 8, -1}, + {7, 5, -1, -1} +}; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + string start = ""; + + int x; + + for(int i=0; i<9; i++){ + cin >> x; + start += x+'0'; + } + + visited[start] = true; + q.push(make_pair(start, 0)); + + while(!q.empty()){ + string now = q.front().first; + int n = q.front().second; + q.pop(); + + if(now == "123456780"){ + cout << n << "\n"; + return 0; + } + + int find; + for(int i=0; i<9; i++){ + if(now[i]=='0'){ + find=i; + break; + } + } + + for(int i=0; i<4; i++){ + if(dx[find][i]==-1) break; + string next; + if(find +#include + +using namespace std; + +struct Node +{ + int x, y, k, turn; +}; + +int K, W, H; +int board[200][200]; +bool visited[200][200][31]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int mx[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; +int my[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; + +queue bfsQueue; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> K >> H >> W; + int i, j; + for (i = 0; i < W; i++) + { + for (j = 0; j < H; j++) + { + cin >> board[i][j]; + } + } + + bfsQueue.push({0, 0, 0, 0}); + visited[0][0][0] = true; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int k = bfsQueue.front().k; + int turn = bfsQueue.front().turn; + bfsQueue.pop(); + + if (x == W - 1 && y == H - 1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < W && ny < H && board[nx][ny] == 0 && !visited[nx][ny][k]) + { + bfsQueue.push({nx, ny, k, turn + 1}); + visited[nx][ny][k] = true; + } + } + if (k < K) + { + for (i = 0; i < 8; i++) + { + int nx = x + mx[i]; + int ny = y + my[i]; + if (nx >= 0 && ny >= 0 && nx < W && ny < H && board[nx][ny] == 0 && !visited[nx][ny][k + 1]) + { + bfsQueue.push({nx, ny, k + 1, turn + 1}); + visited[nx][ny][k + 1] = true; + } + } + } + } + + cout << "-1\n"; + + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" new file mode 100644 index 0000000..44f1401 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int N; +bool visited[100001]; +vector nextv[100001]; +queue< pair > q; +int check_ans[100001]; +int p[100001]; +int order[100001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x >> y; + nextv[x].push_back(y); + nextv[y].push_back(x); + } + + for(int i=0; i> check_ans[i]; + } + + q.push(make_pair(1, 1)); + visited[1] = true; + + while(!q.empty()){ + int x = q.front().first; + int l = q.front().second; + q.pop(); + + for(int next:nextv[x]){ + if(!visited[next]){ + q.push(make_pair(next, l+1)); + visited[next] = true; + p[next] = x; + } + } + } + + for(int i=0; i<=N; i++){ + visited[i] = false; + } + + visited[0] = true; + int porder = -1; + for(int i=0; i order[p[check_ans[i]]]){ + cout << "0\n"; + return 0; + } + + porder = order[p[check_ans[i]]]; + visited[check_ans[i]] = true; + order[check_ans[i]] = i; + } + + cout << "1\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" new file mode 100644 index 0000000..d3aa6ae --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" @@ -0,0 +1,84 @@ +#include +#include + +using namespace std; + +struct node +{ + int x, y, dir, d; +}; + +int N, M; +bool wall[100][100]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +queue bfsQueue; +int sx, sy, sdir, ex, ey, edir; +bool visited[100][100][4]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + cin >> sx >> sy >> sdir >> ex >> ey >> edir; + + ex--; + ey--; + edir--; + bfsQueue.push({sx - 1, sy - 1, sdir - 1, 0}); + visited[sx][sy][sdir] = true; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int dir = bfsQueue.front().dir; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (x == ex && y == ey && dir == edir) + { + cout << d << "\n"; + break; + } + + for (int i = 1; i <= 3; i++) + { + int nx = x + dx[dir] * i; + int ny = y + dy[dir] * i; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (wall[nx][ny]) + break; + + if (!visited[nx][ny][dir]) + { + bfsQueue.push({nx, ny, dir, d + 1}); + visited[nx][ny][dir] = true; + } + } + } + + if (!visited[x][y][(dir + 2) % 4]) + { + bfsQueue.push({x, y, (dir + 2) % 4, d + 1}); + visited[x][y][(dir + 2) % 4] = true; + } + if (!visited[x][y][(dir + 3 - dir % 2 * 2) % 4]) + { + bfsQueue.push({x, y, (dir + 3 - dir % 2 * 2) % 4, d + 1}); + visited[x][y][(dir + 3 - dir % 2 * 2) % 4] = true; + } + } + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" new file mode 100644 index 0000000..ba1b844 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +struct node{ + int x, y, n, c; +}; + +int N, M, P; +string board[1000]; +queue q; +int dps[26]; +int hp; +bool attack[26]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +bool visited[1000][1000][26]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> P; + for(int i=0; i> board[i]; + for(int j=0; j= 'a' && board[i][j] <= 'z'){ + q.push({i,j,0,board[i][j]-'a'}); + visited[i][j][board[i][j]-'a']=true; + } + } + } + char tmp; + for(int i=0; i> tmp; + cin >> dps[tmp-'a']; + } + cin >> hp; + + int finaln = -1; + + while(!q.empty()){ + int x = q.front().x; + int y = q.front().y; + int n = q.front().n; + int c = q.front().c; + q.pop(); + + if(finaln != -1 && n > finaln) break; + + if(board[x][y] == 'B'){ + attack[c] = true; + hp -= dps[c]; + q.push({x,y,n+1,c}); + + if(hp <= 0){ + finaln = n; + } + continue; + } + + for(int i=0; i<4; i++){ + int nx = x+dx[i]; + int ny = y+dy[i]; + if(nx<0 || ny<0 || nx>=N || ny>=M || board[nx][ny]=='X' || visited[nx][ny][c]) continue; + q.push({nx,ny,n+1,c}); + visited[nx][ny][c]=true; + } + } + + int count = 0; + for(int i=0; i<26; i++){ + if(attack[i]) count++; + } + cout << count << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" new file mode 100644 index 0000000..54ceb92 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" @@ -0,0 +1,112 @@ +#include +#include + +using namespace std; + +struct node +{ + int type, x, y, d; +}; + +int T, N, M; +string board[1000]; +bool visited[1000][1000][2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + + for (int t = 0; t < T; t++) + { + queue bfsQueue; + cin >> M >> N; + + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + for (int k = 0; k < 2; k++) + { + visited[i][j][k] = false; + } + } + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (board[i][j] == '*') + { + bfsQueue.push({1, i, j, 0}); + visited[i][j][1] = true; + } + } + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (board[i][j] == '@') + { + bfsQueue.push({0, i, j, 0}); + visited[i][j][0] = true; + } + } + } + + bool flag = false; + + while (!bfsQueue.empty()) + { + int type = bfsQueue.front().type; + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (type == 0 && (x <= 0 || y <= 0 || x >= N - 1 || y >= M - 1)) + { + cout << d + 1 << "\n"; + flag = true; + break; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (type == 1 && nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny][1] && board[nx][ny] != '#') + { + bfsQueue.push({1, nx, ny, d + 1}); + visited[nx][ny][1] = true; + board[nx][ny] = '*'; + } + } + else if (type == 0 && nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny][0] && board[nx][ny] == '.') + { + bfsQueue.push({0, nx, ny, d + 1}); + visited[nx][ny][0] = true; + } + } + } + } + + if (!flag) + { + cout << "IMPOSSIBLE\n"; + } + } + return 0; +} diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" new file mode 100644 index 0000000..03dee78 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; + +int N, M; +bool visited[20001]; +vector edges[20001]; +queue< pair > q; + + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + q.push(make_pair(1, 0)); + visited[1] = true; + int length = 0; + int ans = 1; + int count = 0; + + while(!q.empty()){ + int n = q.front().first; + int d = q.front().second; + q.pop(); + + if(length +#include + +using namespace std; + +struct node{ + int x, y, z, d; +}; + +int L, R, C; +string board[30][30]; +bool visited[30][30][30]; +int startL, startR, startC; +int dx[6] = {0,0,0,0,1,-1}; +int dy[6] = {0,0,1,-1,0,0}; +int dz[6] = {1,-1,0,0,0,0}; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + string tmp; + + while(true){ + cin >> L >> R >> C; + + if(L==0 && R==0 && C==0) break; + + for(int i=0; i> board[i][j]; + for(int k=0; k bfsQueue; + bfsQueue.push({startL, startR, startC, 0}); + visited[startL][startR][startC] = true; + + bool flag = false; + + while(!bfsQueue.empty()){ + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int z = bfsQueue.front().z; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if(board[x][y][z] == 'E'){ + flag = true; + cout << "Escaped in " << d << " minute(s).\n"; + break; + } + + for(int i=0; i<6; i++){ + int nx = x + dx[i]; + int ny = y + dy[i]; + int nz = z + dz[i]; + + if(nx >=0 && ny >= 0 && nz >= 0 && nx < L && ny < R && nz < C && board[nx][ny][nz] != '#' && !visited[nx][ny][nz]){ + visited[nx][ny][nz] = true; + bfsQueue.push({nx, ny, nz, d+1}); + } + } + } + if(!flag){ + cout << "Trapped!\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" new file mode 100644 index 0000000..3c4a4b4 --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using namespace std; + +#define INF 3000000 + +double sx, sy, ex, ey; +int N; + +int main() +{ + cin >> sx >> sy >> ex >> ey; + cin >> N; + N += 2; + + vector > loc(N + 1); + vector dist(N + 1); + fill(dist.begin(), dist.end(), INF); + + loc[1] = make_pair(sx, sy); + loc[N] = make_pair(ex, ey); + + for (int i = 0; i < N - 2; i++) + { + cin >> sx >> sy; + loc[i + 2] = make_pair(sx, sy); + } + + dist[1] = 0.0; + priority_queue > pq; + pq.push(make_pair(0.0, 1)); + + while (!pq.empty()) + { + double d = -pq.top().first; + int now = pq.top().second; + pq.pop(); + + if (dist[now] < d) + continue; + + for (int next = 1; next <= N; next++) + { + bool flag = false; + double x = sqrt(pow(loc[now].first - loc[next].first, 2) + pow(loc[now].second - loc[next].second, 2)); + double y = x / 5; + + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + + if (now != 1 && now != N) + { + if (x >= 50) + { + y = (x - 50) / 5 + 2; + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + } + else + { + y = (50 - x) / 5 + 2; + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + } + } + + if (flag) + { + pq.push(make_pair(-dist[next], next)); + } + } + } + + cout << dist[N] << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" new file mode 100644 index 0000000..6415bd3 --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" @@ -0,0 +1,80 @@ +#include +#include +#include + +using namespace std; + +int N, E; +int node1, node2; + +#define INF 1999999999 +vector > edges[801]; + +vector dijkstra(int s, int N) +{ + vector dist(N + 1); + fill(dist.begin(), dist.end(), INF); + priority_queue > pq; + dist[s] = 0; + pq.push(make_pair(0, s)); + + while (!pq.empty()) + { + int d = -pq.top().first; + int now = pq.top().second; + pq.pop(); + + if (dist[now] < d) + continue; + + for (pair e : edges[now]) + { + int next = e.second; + int c = e.first; + if (dist[now] == INF) + continue; + + if (dist[next] > dist[now] + c) + { + dist[next] = dist[now] + c; + pq.push(make_pair(-dist[next], next)); + } + } + } + return dist; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> E; + + for (int i = 0; i < E; i++) + { + int x, y, z; + cin >> x >> y >> z; + edges[x].push_back({z, y}); + edges[y].push_back({z, x}); + } + cin >> node1 >> node2; + + vector dist = dijkstra(1, N); + vector dist1 = dijkstra(node1, N); + vector dist2 = dijkstra(node2, N); + + long long ans = (long long)dist[node1] + dist1[node2] + dist2[N]; + ans = min(ans, (long long)dist[node2] + dist2[node1] + dist1[N]); + + if (ans >= INF) + { + cout << "-1\n"; + } + else + { + cout << ans << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" new file mode 100644 index 0000000..ee4909b --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" @@ -0,0 +1,62 @@ +#include +#include +#include + +using namespace std; + +#define INF 1000000001 + +int N; +int D[1000][1000]; +vector< pair > edges[1000000]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +priority_queue< pair > pq; +int dist[1000000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> D[i][j]; + } + } + + for(int x=0; x= N || ny >= N) continue; + + edges[x*N+y].push_back(make_pair(nx*N+ny, abs(D[x][y]-D[nx][ny]))); + } + } + } + + for(int i=0; i<=N*N-1; i++) dist[i] = INF; + dist[0] = 0; + pq.push(make_pair(0, 0)); + + while(!pq.empty()){ + int cost = -pq.top().first; + int x = pq.top().second; + pq.pop(); + + if(dist[x] < cost) continue; + + for(pair next : edges[x]){ + if(dist[next.first] > max(next.second, cost)){ + dist[next.first] = max(next.second, cost); + pq.push(make_pair(-dist[next.first], next.first)); + } + } + } + + cout << dist[N*N-1] << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" "b/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" index 2a6a3fc..990ba1d 100644 --- "a/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" +++ "b/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" @@ -25,37 +25,43 @@ int main() int res3 = CCW(x3, y3, x4, y4, x1, y_1); int res4 = CCW(x3, y3, x4, y4, x2, y2); - if (res1 * res2 < 0) - { - if (res3 * res4 <= 0) - { - cout << "1\n"; - return 0; + if(res1==0 && res2==0 && res3==0 && res4==0){ + if(x1>x2){ + swap(x1, x2); + swap(y_1, y2); } - } - else if (res1 == 0 && res2 == 0) - { - if ((x3 >= x1 && x3 <= x2) || (x3 >= x2 && x3 <= x1)) - { - if ((y3 >= y_1 && y3 <= y2) || (y3 >= y2 && y3 <= y_1)) - { + if(x3>x4){ + swap(x3, x4); + swap(y3, y4); + } + if(x1>x3){ + swap(x1, x3); swap(y_1, y3); swap(x2, x4); swap(y2, y4); + } + + if(x1==x2 && x2==x3 &&x3==x4){ + if(y_1>y2) swap(y_1, y2); + if(y3>y4) swap(y3, y4); + if(y_1>y3){ + swap(y_1, y3); + swap(y2, y4); + } + if(y3>=y_1&&y3<=y2){ cout << "1\n"; return 0; } - } - if ((x4 >= x1 && x4 <= x2) || (x4 >= x2 && x4 <= x1)) - { - if ((y4 >= y_1 && y4 <= y2) || (y4 >= y2 && y4 <= y_1)) - { - cout << "1\n"; + else{ + cout << "0\n"; return 0; } } + + if(x3>=x1&&x3<=x2){ + cout << "1\n"; + return 0; + } } - else if (res1 == 0 || res2 == 0) - { - if (res3 * res4 <= 0) - { + else{ + if(res1*res2<=0 && res3*res4<=0){ cout << "1\n"; return 0; } diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" new file mode 100644 index 0000000..31f2305 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int G; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> G; + + int left = 1, right = 1; + bool flag = false; + + while (right <= 100000) + { + long long now = (long long)right * right - (long long)left * left; + if (now == G) + { + flag = true; + cout << right << "\n"; + left++; + } + else if (now < G) + { + right++; + } + else + { + left++; + } + } + + if (!flag) + { + cout << "-1\n"; + } + + return 0; +} diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" new file mode 100644 index 0000000..b7bdc07 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int N, D, K, C; +int include[3001]; +int A[3000000]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int i, x; + int count = 0; + int ans = 0; + int kind = 0; + + cin >> N >> D >> K >> C; + include[C]++; + kind = 1; + + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + + int left = 0, right = 0; + if (include[A[0]] == 0) + { + kind++; + } + include[A[0]]++; + count = 1; + ans = kind; + + while (left < N) + { + if (count <= K) + { + right++; + count++; + if (right == N) + right = 0; + + if (include[A[right]] == 0) + { + kind++; + } + include[A[right]]++; + } + else + { + include[A[left]]--; + if (include[A[left]] == 0) + { + kind--; + } + + left++; + count--; + } + + if (count == K) + ans = max(ans, kind); + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" new file mode 100644 index 0000000..cfba708 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +long long N, M; +long long A[100000]; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + int ans = 2000000000; + + cin >> N >> M; + + for(int i=0; i> A[i]; + } + + sort(A, A+N); + + int left = 0, right = 0; + int now = 0; + + while(left < N && right < N && left <= right){ + + now = A[right] - A[left]; + + if(now < M){ + right++; + } + else{ + ans = min(ans, now); + left++; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" new file mode 100644 index 0000000..cce8a23 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; + +int N; +long long A[4000][4]; +vector B; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for(int i=0; i> A[i][j]; + } + } + + for(int i=0; i::iterator low = lower_bound(B.begin(), B.end(), -now); + vector::iterator high = upper_bound(B.begin(), B.end(), -now); + + if(*low == -now){ + ans += high-low; + } + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\247\265/1351.cpp" "b/baekjoon/\353\247\265/1351.cpp" new file mode 100644 index 0000000..d86f1b4 --- /dev/null +++ "b/baekjoon/\353\247\265/1351.cpp" @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +long long N, P, Q; +map d; + +long long dp(long long n){ + if(d.find(n) == d.end()){ + return d[n] = dp(n/P) + dp(n/Q); + } + return d[n]; +} + + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> P >> Q; + + d[0] = 1; + + cout << dp(N) << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" new file mode 100644 index 0000000..2bc1ea1 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int main(){ + int N; + string str; + + cin >> N; + for(int i=0; i> str; + + while(str.length() > 1){ + cout << str << "\n"; + if(str.substr(str.length()-2, 2) == "01" && (str.length() == 2 || str[str.length()-3] == '1')){ + str = str.substr(0, str.length() - 2); + } + else{ + bool start = false, end = false; + int count = 0; + int idx = str.length()-1; + + while(idx>=0 && str[idx] == '1'){ + idx--; + end = true; + } + + for(; idx>=0; idx--){ + if(str[idx] == '0') count++; + else break; + } + + if(idx>=0 && str[idx] == '1') start = true; + if(start && end && count >=2){ + str = str.substr(0, idx); + } + else break; + } + } + + if(str.length() > 0){ + cout << "NO\n"; + } + else{ + cout << "YES\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" new file mode 100644 index 0000000..bd5b999 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" @@ -0,0 +1,35 @@ +#include + +using namespace std; + +string str; + +int main(){ + + cin >> str; + + if(str[0] != str[str.length()-1]){ + cout << str.length() << "\n"; + return 0; + } + + bool pelindrome = true; + bool all_same = true; + + for(int i=0; i + +using namespace std; + +string S, K; +int kmp[200000]; +int D[200000]; + +int main(){ + cin >> S >> K; + for(int i=0; i 0){ + j = kmp[j-1]; + } + + if(K[j]==K[i]){ + kmp[i] = j++; + } + } + + if(S[0] == K[0]) D[0] = 1; + + j = 0; + for(int i=0; i= '0' && S[i] <= '9'){ + D[i] = D[i-1]; + continue; + } + + while(K[j] != S[i] && j > 0){ + j = kmp[j-1] + 1; + } + if(K[j] == S[i]) D[i]=++j; + + if(D[i] == K.length()){ + cout << "1\n"; + return 0; + } + } + + cout << "0\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" new file mode 100644 index 0000000..26afd93 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" @@ -0,0 +1,27 @@ +#include + +using namespace std; + +string S; +int K; +int fail[500000]; + +int main(){ + + cin >> S >> K; + + int j = 0; + + for(int i=1; i0 && S[i]!=S[j]){ + j = fail[j-1]; + } + if(S[i]==S[j]){ + fail[i] = ++j; + } + } + + cout << S.length() + (S.length()-fail[S.length()-1])*(K-1) << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" new file mode 100644 index 0000000..ef3c09a --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +bool notPrime[1003002]; + +void goprime() +{ + notPrime[1] = true; + for (long long i = 2; i <= 500000; i++) + { + if (notPrime[i]) + continue; + for (long long j = 2; i * j <= (long long)1003001; j++) + { + notPrime[i * j] = true; + } + } +} + +bool pelindrum(string s, int n) +{ + if (n == 1 || n == 0) + return true; + + if (s[0] == s[n - 1]) + return pelindrum(s.substr(1, n - 2), n - 2); + else + return false; +} + +int main() +{ + goprime(); + + int N; + cin >> N; + + for (int i = N; i <= 1003001; i++) + { + if (notPrime[i]) + continue; + + string s = to_string(i); + if (pelindrum(s, s.length())) + { + cout << i << "\n"; + break; + } + } + + return 0; +} diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" new file mode 100644 index 0000000..074d8b4 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include + +using namespace std; + +int T; +string A, W, S; +int kmp[50000]; +int ans[100]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0; t> A >> W >> S; + + for(int i=0; i cmap; + for(int i=0; i0 && W[i] != W[j]){ + j = kmp[j-1]; + } + if(W[i] == W[j]) + kmp[i] = ++j; + } + + j=0; + for(int i=0; i0 && S[i] != W[j]){ + j = kmp[j-1]; + } + + if(S[i] == W[j]){ + if(j==W.length()-1){ + ans[shift]++; + j = kmp[j]; + } + else j++; + } + } + } + + int count = 0; + vector ans_list; + + for(int i=0; i + +using namespace std; + +string A,B,C; +int DP[101][101][101]; +int ans = 0; + +int solve(int a, int b, int c){ + if(a>=A.length() || b>=B.length() || c>=C.length()) return 0; + if(DP[a][b][c] != -1) return DP[a][b][c]; + + int result = 0; + result = max(max(solve(a+1, b, c), solve(a, b+1, c)), solve(a,b,c+1)); + result = max(result, max(max(solve(a+1, b+1, c), solve(a+1, b, c+1)), solve(a, b+1, c+1))); + bool check = (A[a] == B[b] && B[b] == C[c]); + result = max(result, check + solve(a+1, b+1, c+1)); + return DP[a][b][c] = result; +} + +int main(){ + cin >> A >> B >> C; + + for(int i=0; i + +using namespace std; + +string str; +int DP[500][500]; + +int main(){ + cin >> str; + + for(int j=1; j=0; i--){ + DP[i][j] = max(DP[i+1][j], DP[i][j-1]); + + if(str[i] == 'a' && str[j] == 't'){ + DP[i][j] = max(DP[i][j], DP[i+1][j-1] + 2); + } + else if(str[i] == 'g' && str[j] == 'c'){ + DP[i][j] = max(DP[i][j], DP[i+1][j-1] + 2); + } + + for(int k=i+1; k+1 + +using namespace std; + +string str; + +int main(){ + + cin >> str; + char pre = '?'; + bool start = false; + bool end = false; + str += "??"; + for(int i=0; i +#include +#include + +using namespace std; + +vector stack; +vector ans; +int par[201]; +string s; +int include[201]; + +void solve(int index, int n) +{ + if (index == s.length()) + { + if (n > 0) + { + string temp = ""; + for (int i = 0; i < s.length(); i++) + { + if (include[i]) + temp += s[i]; + } + ans.push_back(temp); + } + return; + } + + if (s[index] == '(') + { + include[index] = true; + include[par[index]] = true; + solve(index + 1, n); + include[index] = false; + include[par[index]] = false; + solve(index + 1, n + 1); + } + else if (s[index] != ')') + { + include[index] = true; + solve(index + 1, n); + } + else + { + include[index] = include[par[index]]; + solve(index + 1, n); + } +} + +int main() +{ + cin >> s; + fill(par, par + s.length(), -1); + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '(') + { + stack.push_back(i); + } + else if (s[i] == ')') + { + int t = stack.back(); + stack.pop_back(); + par[t] = i; + par[i] = t; + } + } + + solve(0, 0); + sort(ans.begin(), ans.end()); + ans.erase(unique(ans.begin(), ans.end()), ans.end()); + + for (string a : ans) + { + cout << a << "\n"; + } + return 0; +} diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" new file mode 100644 index 0000000..a53e62a --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +int N, M; +string tmp[1000]; +string board[1000]; +bool DP[1000][1000]; + + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> tmp[i]; + } + + for(int i=0; i=0; j--){ + board[i] += tmp[j][i]; + } + } + + sort(board, board+M); + + int ans = 0; + bool flag = true; + + for(int i=1; i= 1 and len(ipv6[i]) <= 3: + ipv6[i] = "0" * (4-len(ipv6[i])) + ipv6[i] + elif len(ipv6[i]) == 0: + ipv6.pop(i) + for j in range(8-len(ipv6)): + ipv6.insert(i, "0000") + +for i in range(7): + print(ipv6[i], end=':') +print(ipv6[7]) diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" new file mode 100644 index 0000000..54dfba4 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +using namespace std; + +int T, N; +string command; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0;t> command; + cin >> N; + string tmp; + cin >> tmp; + list mylist; + + int key = 0; + + for(int i=1; tmp[i] != ']'; i++){ + if(tmp[i] >= '0' && tmp[i] <= '9'){ + key = key*10 + tmp[i]-'0'; + } + else if(tmp[i] == ','){ + mylist.push_back(key); + key = 0; + } + } + + if(key!=0) mylist.push_back(key); + + for(int i=0; i::iterator it; + if(!reverse){ + it = mylist.begin(); + } + else{ + it = mylist.end(); + it--; + } + it = mylist.erase(it); + } + } + + if(!flag) continue; + vector ans; + for (int const &c: mylist) { + ans.push_back(c); + } + + cout << "["; + if(!reverse){ + for(int i=0; i=0; i--){ + cout << ans[i]; + if(i!=0) cout << ","; + } + } + cout << "]\n"; + } + return 0; +} diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" new file mode 100644 index 0000000..d9c2944 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" @@ -0,0 +1,58 @@ +#include + +using namespace std; + +string str; +bool notprime[100000]; + +int main(){ + + notprime[0] = true; + notprime[1] = true; + + for(int i=2; i<100000; i++){ + if(notprime[i]) continue; + + for(int j=2; i*j<100000; j++){ + if(!notprime[i*j]){ + notprime[i*j] = true; + } + } + } + + while(true){ + cin >> str; + if(str=="0") break; + + int ans = 0; + + for(int i=0; i + +using namespace std; + +int N; +string str; +int alpha[26]; + +void solve(int n, string s){ + + if(n==str.length()){ + cout << s << "\n"; + return; + } + + for(int i=0; i<26; i++){ + if(alpha[i] > 0){ + alpha[i]--; + solve(n+1, s + (char)('a'+i)); + alpha[i]++; + } + } +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + for(int n=0; n> str; + for(int i=0; i<26; i++) + alpha[i] = 0; + + for(int i=0; i +#include + +using namespace std; + +vector stack; +int T; +string word[10]; +int alpha[26]; +int total = 0; +bool final = false; + +int solve(int now, int n, string s, bool flag){ + + if(final) return 0; + + if(n == word[now].length()){ + if(flag){ + stack.push_back(s); + total++; + final = true; + return 1; + } + return 0; + } + + int res = 0; + for(int i=0; i<26; i++){ + if(alpha[i] > 0){ + if(!flag && word[now][n] - 'A' <= i){ + alpha[i]--; + res += solve(now, n+1, s+char('A'+i), word[now][n]-'A' != i); + alpha[i]++; + } + else if(flag){ + alpha[i]--; + res += solve(now, n+1, s+char('A'+i), true); + alpha[i]++; + } + } + } + + return res; +} + +int main(){ + + cin >> T; + for(int i=0; i> word[i]; + for(int j=0; j<26; j++){ + alpha[j] = 0; + } + for(unsigned int j=0; j0){ + cout << stack[0] << "\n"; + } + else{ + cout << word[i] << "\n"; + } + } + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" new file mode 100644 index 0000000..3b4698f --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" @@ -0,0 +1,123 @@ +#include +#include + +using namespace std; +char board[10][10]; +int ans = 100; + +bool check(int i, int j, int size) +{ + if (i + size - 1 >= 10 || j + size - 1 >= 10) + return false; + + bool flag = true; + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1 && flag; y++) + { + if (board[x][y] == '0') + flag = false; + } + } + return flag; +} + +void cover(int i, int j, int size) +{ + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1; y++) + { + board[x][y] = '0'; + } + } +} + +void uncover(int i, int j, int size) +{ + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1; y++) + { + board[x][y] = '1'; + } + } +} + +void dfs(int i, int j, int count, int paper, int arg[]) +{ + int size_paper[5] = {arg[0], arg[1], arg[2], arg[3], arg[4]}; + + if (count == 0) + { + ans = min(ans, paper); + return; + } + + if (i == 10) + { + return; + } + + if (ans <= paper) + return; + + int nexti = i; + int nextj = j + 1; + if (nextj == 10) + { + nexti++; + nextj = 0; + } + + if (board[i][j] == '1') + { + for (int size = 5; size >= 1; size--) + { + if (check(i, j, size) && size_paper[size - 1] < 5) + { + //cout << i << " " << j << " " << size << "\n"; + cover(i, j, size); + int arr[5] = {size_paper[0], size_paper[1], size_paper[2], size_paper[3], size_paper[4]}; + arr[size - 1]++; + //cout << i << " " << j << " " << count - size * size << " " << paper + 1 << " " << arr[0] << arr[1] << arr[2] << arr[3] << arr[4] << "\n"; + dfs(nexti, nextj, count - size * size, paper + 1, arr); + uncover(i, j, size); + } + } + } + else + { + dfs(nexti, nextj, count, paper, size_paper); + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int i, j; + int count = 0; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + cin >> board[i][j]; + if (board[i][j] == '1') + count++; + } + } + + int arr[5] = {0, 0, 0, 0, 0}; + dfs(0, 0, count, 0, arr); + + if (ans == 100) + { + cout << "-1\n"; + } + else + { + cout << ans << "\n"; + } + return 0; +} diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" new file mode 100644 index 0000000..1407c2b --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int R, C; +string board[20]; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +bool include[26]; +int ans = 0; + +void dfs(int i, int j, int dist) +{ + ans = max(ans, dist); + for (int x = 0; x < 4; x++) + { + int nx = i + dx[x]; + int ny = j + dy[x]; + if (nx >= 0 && ny >= 0 && nx < R && ny < C && !include[board[nx][ny] - 'A']) + { + include[board[nx][ny] - 'A'] = true; + dfs(nx, ny, dist + 1); + include[board[nx][ny] - 'A'] = false; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> R >> C; + + int i, j; + for (i = 0; i < R; i++) + { + cin >> board[i]; + } + + include[board[0][0] - 'A'] = true; + dfs(0, 0, 1); + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" new file mode 100644 index 0000000..51e757c --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" @@ -0,0 +1,57 @@ +#include + +using namespace std; +int N; + +bool check(string str) +{ + int n = 0; + + for (int i = 0; i < str.length(); i++) + { + n = n * 10 + (str[i] - '0'); + } + + if (n < 2) + return false; + + bool flag = true; + for (int i = 2; i <= n / 2; i++) + { + if (n % i == 0) + { + flag = false; + break; + } + } + return flag; +} + +void solve(int n, string str) +{ + //cout << n << " " << str << "\n"; + if (n == N) + { + cout << str << "\n"; + return; + } + + for (int i = 0; i < 10; i++) + { + string now = str + char('0' + i); + if (check(now.substr(0, n + 1))) + { + solve(n + 1, now); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> N; + solve(0, ""); + return 0; +} diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" new file mode 100644 index 0000000..30ce251 --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" @@ -0,0 +1,87 @@ +#include + +using namespace std; + +string board[9]; +bool isEnd = false; + +bool check(int x, int y, int n) +{ + for (int i = 0; i < 9; i++) + { + if (board[x][i] == n + '0') + return false; + if (board[i][y] == n + '0') + return false; + } + + for (int i = x - x % 3; i < x - x % 3 + 3; i++) + { + for (int j = y - y % 3; j < y - y % 3 + 3; j++) + { + if (board[i][j] == n + '0') + return false; + } + } + + return true; +} + +void solve(int x, int y) +{ + if (x == 9 && y == 0) + { + for (int i = 0; i < 9; i++) + { + cout << board[i] << "\n"; + } + isEnd = true; + return; + } + + if (isEnd) + return; + + int nextx = x; + int nexty = y + 1; + if (nexty == 9) + { + nextx++; + nexty = 0; + } + + if (board[x][y] == '0') + { + for (int i = 1; i <= 9; i++) + { + if (check(x, y, i)) + { + board[x][y] = '0' + i; + solve(nextx, nexty); + board[x][y] = '0'; + } + } + + return; + } + + solve(nextx, nexty); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int i, j; + + for (i = 0; i < 9; i++) + { + cin >> board[i]; + } + + solve(0, 0); + + return 0; +} diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" new file mode 100644 index 0000000..4580917 --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" @@ -0,0 +1,94 @@ +#include + +using namespace std; + +int board[9][9]; +bool isEnd = false; + +bool check(int x, int y, int n) +{ + for (int i = 0; i < 9; i++) + { + if (board[x][i] == n) + return false; + if (board[i][y] == n) + return false; + } + + for (int i = x - x % 3; i < x - x % 3 + 3; i++) + { + for (int j = y - y % 3; j < y - y % 3 + 3; j++) + { + if (board[i][j] == n) + return false; + } + } + + return true; +} + +void solve(int x, int y) +{ + if (x == 9 && y == 0) + { + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + cout << board[i][j] << " "; + } + cout << "\n"; + } + isEnd = true; + return; + } + + if (isEnd) + return; + + int nextx = x; + int nexty = y + 1; + if (nexty == 9) + { + nextx++; + nexty = 0; + } + + if (board[x][y] == 0) + { + for (int i = 1; i <= 9; i++) + { + if (check(x, y, i)) + { + board[x][y] = i; + solve(nextx, nexty); + board[x][y] = 0; + } + } + + return; + } + + solve(nextx, nexty); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int i, j; + + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + cin >> board[i][j]; + } + } + + solve(0, 0); + + return 0; +} diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" new file mode 100644 index 0000000..80c72ed --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" @@ -0,0 +1,52 @@ +#include + +using namespace std; +int N; +string ans = ""; + +void solve(int n, string str) +{ + //cout << n << " " << str << "\n"; + + if (n == N) + { + if (ans == "" || ans > str) + ans = str; + return; + } + + if (ans.length() == N && ans < str) + return; + + for (int i = 1; i <= 3; i++) + { + string now = str + char(i + '0'); + bool flag = true; + for (int j = 1; j <= now.length() / 2; j++) + { + //cout << now.substr(now.length() - j, j) << "/" << now.substr(now.length() - 2 * j, j) << "\n"; + if (now.substr(now.length() - j, j) == now.substr(now.length() - 2 * j, j)) + { + flag = false; + break; + } + } + if (flag) + { + solve(n + 1, now); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + solve(0, ""); + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" new file mode 100644 index 0000000..6efb3c7 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" @@ -0,0 +1,101 @@ +#include +#include + +using namespace std; + +typedef struct node +{ + int n; + int c; + + bool operator<(const node &a) const + { + return c > a.c; + } +} node; + +int N, M, K; +int P[10001]; +int A[10001]; +priority_queue pq; + +int findP(int n) +{ + if (P[n] == n) + { + return n; + } + + if (P[P[n]] == P[n]) + { + return P[n]; + } + + A[P[n]] += A[n]; + + return findP(P[n]); +} + +void Union(int x, int y) +{ + x = findP(x); + y = findP(y); + + if (x == y) + return; + + if (x < y) + { + swap(x, y); + } + + A[y] += A[P[x]]; + P[P[x]] = P[y]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int x, y; + + cin >> N >> M >> K; + for (int i = 1; i <= N; i++) + { + cin >> x; + pq.push({i, x}); + P[i] = i; + A[i] = 1; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + Union(x, y); + } + + int ans = 0; + + while (A[0] < N && ans < K && !pq.empty()) + { + node top = pq.top(); + pq.pop(); + if (findP(top.n) != 0) + { + Union(0, top.n); + ans += top.c; + } + } + + if (A[0] != N || ans > K) + { + cout << "Oh no\n"; + } + else + { + cout << ans << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" new file mode 100644 index 0000000..aa8bfda --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" @@ -0,0 +1,103 @@ +#include + +using namespace std; +int N, M; +string board[500]; +pair p[501][501]; +int sum[501][501]; + +pair getp(int i, int j) +{ + if (p[i][j] == make_pair(i, j)) + return make_pair(i, j); + + if (p[p[i][j].first][p[i][j].second] == p[i][j]) + return p[i][j]; + + sum[p[i][j].first][p[i][j].second] += sum[i][j]; + + return p[i][j] = getp(p[i][j].first, p[i][j].second); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + p[i][j] = make_pair(i, j); + sum[i][j] = 1; + } + } + + p[N][M] = make_pair(N, M); + + for (int x = 0; x < N; x++) + { + for (int y = 0; y < M; y++) + { + + int nx, ny; + if (board[x][y] == 'D') + { + nx = x + 1; + ny = y; + } + else if (board[x][y] == 'U') + { + nx = x - 1; + ny = y; + } + else if (board[x][y] == 'R') + { + nx = x; + ny = y + 1; + } + else + { + nx = x; + ny = y - 1; + } + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (getp(x, y) != getp(nx, ny)) + { + + if (getp(x, y) == make_pair(N, M)) + { + sum[N][M] += sum[p[nx][ny].first][p[nx][ny].second]; + p[p[nx][ny].first][p[nx][ny].second] = make_pair(N, M); + } + else if (getp(nx, ny) == make_pair(N, M)) + { + sum[N][M] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(N, M); + } + else + { + sum[p[nx][ny].first][p[nx][ny].second] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(p[nx][ny].first, p[nx][ny].second); + } + } + } + else + { + if (getp(x, y) != make_pair(N, M)) + { + sum[N][M] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(N, M); + } + } + } + } + + cout << sum[N][M] << "\n"; + return 0; +} diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" new file mode 100644 index 0000000..e0eb027 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int N, M; +int p[500000]; + +int getp(int n) +{ + if (p[n] == n) + return n; + return p[n] = getp(p[n]); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + int x, y; + bool flag = false; + int ans = 0; + for (int i = 0; i < N; i++) + { + p[i] = i; + } + for (int i = 0; i < M; i++) + { + cin >> x >> y; + if (getp(x) != getp(y)) + { + p[p[x]] = p[y]; + } + else if (!flag) + { + flag = true; + ans = i + 1; + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" new file mode 100644 index 0000000..da25290 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" @@ -0,0 +1,63 @@ +#include +#include + +using namespace std; + +int T, N; +int p[20001]; +int dist[20001]; + +int getP(int x) +{ + if (p[x] == x) + return x; + + int t = getP(p[x]); + dist[x] += dist[p[x]]; + return p[x] = t; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + char c; + int x, y; + for (int t = 0; t < T; t++) + { + cin >> N; + for (int i = 1; i <= N; i++) + { + dist[i] = 0; + p[i] = i; + } + + while (true) + { + cin >> c; + if (c == 'O') + break; + + if (c == 'E') + { + cin >> x; + getP(x); + cout << dist[x] << "\n"; + } + else if (c == 'I') + { + cin >> x >> y; + getP(x); + getP(y); + + dist[x] += abs(x - y) % 1000; + p[x] = y; + } + } + } + + return 0; +} diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" new file mode 100644 index 0000000..8244f48 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +string alpha = "helowrd"; +int idx[10] = {0,1,2,2,3,4,3,5,2,6}; +int digit[11]; +bool used[11]; +int N; +bool flag = false; + +void solve(int n){ + + if(flag) return; + if(n==7){ + + int answer = 0; + + for(int i=0; i<5; i++){ + answer += digit[idx[i]]*pow(10, 4-i) + digit[idx[i+5]]*pow(10, 4-i); + } + + if(answer == N){ + flag = true; + + cout << " "; + for(int i=0; i<5; i++){ + cout << digit[idx[i]]; + } + cout << "\n+ "; + for(int i=5; i<10; i++){ + cout << digit[idx[i]]; + } + cout << "\n-------\n"; + printf("%7d\n", N); + } + + return; + } + + for(int i=0; i<10; i++){ + if((n==0 || n==4) && i==0){ + continue; + } + + if(!used[i]){ + digit[n] = i; + used[i] = true; + solve(n+1); + used[i] = false; + } + } +} + +int main(){ + + cin >> N; + + solve(0); + + if(!flag){ + cout << "No Answer\n"; + } + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" new file mode 100644 index 0000000..2a4a66a --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int countA[26]; +long long ans = 0; +string str; + +void solve(int n, string s){ + if(n==str.length()){ + ans++; + return; + } + + for(int i=0; i<26; i++){ + if(s.length()==0 || s[s.length()-1] != char(i+'a')) + if(countA[i]>0){ + countA[i]--; + solve(n+1, s+(char)(i+'a')); + countA[i]++; + } + } +} + +int main(){ + + cin >> str; + + for(int i=0; i + +using namespace std; + +int N; +string str; + +int op(int x, int y, char c){ + if(c=='+') return x+y; + else if(c=='-') return x-y; + else return x*y; +} + +int solve(int idx, int tmp){ + + if(idx>=N) return tmp; + + int res = -2147483647; + + int y = str[idx+1] - '0'; + res = max(res, solve(idx+2, op(tmp, y, str[idx]))); + + if(idx+4 <= N){ + int z = str[idx+3] - '0'; + res = max(res, solve(idx+4, op(tmp, op(y, z, str[idx+2]), str[idx]))); + } + + cout << res << "\n"; + return res; +} + +int main(){ + + cin >> N; + cin >> str; + cout << solve(1, str[0]-'0') << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" new file mode 100644 index 0000000..513e055 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" @@ -0,0 +1,93 @@ +#include +#include + +using namespace std; + +bool include[5][5]; +string board[5]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +int ans; + +bool adjacent(){ + bool visited[5][5]; + + for(int i=0; i<5; i++){ + for(int j=0; j<5; j++){ + visited[i][j] = false; + } + } + + queue < pair > q; + for(int i=0; i<5 && q.empty(); i++){ + for(int j=0; j<5; j++){ + if(include[i][j]){ + q.push(make_pair(i,j)); + visited[i][j] = true; + break; + } + } + } + + int count = 1; + + while(!q.empty() && count < 7){ + int x = q.front().first; + int y = q.front().second; + q.pop(); + + for(int i=0; i<4; i++){ + int nx = x+dx[i]; + int ny = y+dy[i]; + if(nx>=0 && ny >=0 && nx < 5 && ny < 5 && !visited[nx][ny] && include[nx][ny]){ + visited[nx][ny] = true; + q.push(make_pair(nx,ny)); + count++; + } + } + } + + if(count == 7)return true; + return false; +} + +void brute(int i, int j, int selected, int y, int s){ + if(selected == 7){ + if(y=5){ + i++; + j=0; + } + + if(i>=5){ + return; + } + + int ny = y; + int ns = s; + + include[i][j] = true; + if(board[i][j] == 'Y') ny++; + else ns++; + brute(i, j+1, selected+1, ny, ns); + include[i][j] = false; + brute(i, j+1, selected, y, s); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + for(int i=0; i<5; i++) cin >> board[i]; + + brute(0, 0, 0, 0, 0); + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" new file mode 100644 index 0000000..a8cfe1b --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int A[10000][6]; +int N; + +int getbiggest(int n, int i){ + if(i==0 || i==5) return max(max(A[n][1], A[n][2]), max(A[n][3], A[n][4])); + if(i==2 || i==4) return max(max(A[n][0], A[n][1]), max(A[n][3], A[n][5])); + return max(max(A[n][0], A[n][2]), max(A[n][4], A[n][5])); +} + +int oposite[6] = {5, 3, 4, 1, 2, 0}; + +int solve(int n, int top){ + + if(n==N) return 0; + + int down; + + for(int i=0; i<6; i++){ + if(A[n-1][top] == A[n][i]){ + down = i; + break; + } + } + + return solve(n+1, oposite[down]) + getbiggest(n, oposite[down]); + +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> A[i][j]; + } + } + + int ans = 0; + + for(int i=0; i<6; i++){ + ans = max(ans, solve(1, i) + getbiggest(0, i)); + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" new file mode 100644 index 0000000..9d32103 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int N, M; +bool isOpen[21]; +int T[21]; + +int solve(int n, int ans){ + if(n==M) return ans; + + int x, y; + for(int i=1; i<=N; i++){ + if(isOpen[i]){ + x=i; + break; + } + } + + for(int i=x+1; i<=N; i++){ + if(isOpen[i]){ + y=i; + break; + } + } + + if(T[n] == x || T[n] == y) return solve(n+1, ans); + if(T[n] < x){ + isOpen[x] = false; + isOpen[T[n]] = true; + + int res = solve(n+1, ans + x-T[n]); + isOpen[x] = true; + isOpen[T[n]] = false; + return res; + } + if(T[n] > y){ + isOpen[y] = false; + isOpen[T[n]] = true; + + int res = solve(n+1, ans + T[n]-y); + isOpen[y] = true; + isOpen[T[n]] = false; + return res; + } + + int d1, d2; + + isOpen[x] = false; + isOpen[T[n]] = true; + d1 = solve(n+1, ans + T[n]-x); + isOpen[x] = true; + isOpen[y] = false; + d2 = solve(n+1, ans + y - T[n]); + if(d1 < d2){ + isOpen[x] = true; + isOpen[T[n]] = false; + return d1; + } + else{ + isOpen[y] = true; + isOpen[T[n]] = false; + return d2; + } +} + +int main(){ + cin >> N; + + int x; + for(int i=0; i<2; i++){ + cin >> x; + isOpen[x] = true; + } + + cin >> M; + + for(int i=0; i> T[i]; + } + + cout << solve(0, 0) << "\n"; + + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" new file mode 100644 index 0000000..d188783 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int check[15]; +int N; +int ans = 0; + +void solve(int n){ + + if(n==N){ + ans++; + return; + } + + bool possible[15]; + for(int i=0; i=0) + possible[check[i]-(n-i)]=false; + } + for(int i=0; i> N; + solve(0); + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" new file mode 100644 index 0000000..538675e --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + +struct Move +{ + int x, y, turn; + unsigned int key; +}; + +bool visited[50][50][128]; +string board[50]; +int N, M; +queue bfsQueue; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + for (j = 0; j < M; j++) + { + if (board[i][j] == '0') + { + bfsQueue.push({i, j, 0, 0}); + visited[i][j][0] = true; + } + } + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int turn = bfsQueue.front().turn; + unsigned int key = bfsQueue.front().key; + bfsQueue.pop(); + + if (board[x][y] == '1') + { + cout << turn << "\n"; + return 0; + } + + if (board[x][y] >= 'a' && board[x][y] <= 'f') + { + unsigned int k = 1 << (board[x][y] - 'a'); + key = (key | k); + } + else if (board[x][y] >= 'A' && board[x][y] <= 'F') + { + unsigned int k = 1 << (board[x][y] - 'A'); + if ((key & k) != k) + continue; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !visited[nx][ny][key] && board[nx][ny] != '#') + { + bfsQueue.push({nx, ny, turn + 1, key}); + visited[nx][ny][key] = true; + } + } + } + + cout << "-1\n"; + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" new file mode 100644 index 0000000..3a22595 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace std; + +int N; +unsigned int board[21]; +int ans = 400; + +void brute(int n) +{ + if (n == N) + { + int c = 0; + for (int i = 0; i < N; i++) + { + int t = 0; + int h = 0; + for (int j = 0; j < N; j++) + { + if ((board[j] & (1 << i)) == (1 << i)) + t++; + else + h++; + } + t = min(h, t); + c += t; + } + ans = min(ans, c); + return; + } + + board[n] = ~board[n]; + brute(n + 1); + board[n] = ~board[n]; + brute(n + 1); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + string s; + int i, j; + for (i = 0; i < N; i++) + { + cin >> s; + for (j = 0; j < N; j++) + { + if (s[j] == 'T') + board[i] |= (1 << j); + } + } + + brute(0); + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" new file mode 100644 index 0000000..89dead9 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" @@ -0,0 +1,23 @@ +#include + +using namespace std; +int a[1 << 20], x; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + while (cin >> x) + { + int q = x / 32; + int r = x % 32; + + if ((a[q] & (1 << r)) != (1 << r)) + { + cout << x << " "; + a[q] |= (1 << r); + } + } + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" new file mode 100644 index 0000000..5cd08e3 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int N; +int dp_table[1000001][4]; + +int dp(int n, int status) +{ + if (n == 0) + { + if (status == 1) + return 1; + else + return 0; + } + else if (n == 1) + { + if (status == 1) + return 2; + else + return 1; + } + + int res = dp_table[n][status]; + if (res != 0) + return res; + + switch (status) + { + case 1: + res = ((long long)dp(n - 1, 1) * 2 + dp(n - 1, 2) + dp(n - 1, 3) + dp(n - 2, 1)) % 1000000007; + break; + case 2: + res = ((long long)dp(n - 1, 1) + dp(n - 1, 3)) % 1000000007; + break; + case 3: + res = ((long long)dp(n - 1, 1) + dp(n - 1, 2)) % 1000000007; + break; + } + + return dp_table[n][status] = res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + cout << dp(N, 1) << "\n"; + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" new file mode 100644 index 0000000..25550f6 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int N, M; +unsigned int alpha[10000]; +unsigned int know = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> N >> M; + + int i, j, x; + unsigned char c; + for (i = 'a' - 'a'; i <= 'z' - 'a'; i++) + { + know |= (1 << i); + } + + for (i = 0; i < N; i++) + { + string s; + cin >> s; + for (j = 0; j < s.length(); j++) + { + alpha[i] |= (1 << (s[j] - 'a')); + } + } + + for (i = 0; i < M; i++) + { + cin >> x >> c; + if (x == 1 && (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u')) + { + know &= ~(1 << (c - 'a')); + } + else if (x == 2) + { + know |= (1 << (c - 'a')); + } + + int count = 0; + for (j = 0; j < N; j++) + { + if ((know & alpha[j]) == alpha[j]) + count++; + } + cout << count << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" new file mode 100644 index 0000000..47129aa --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" @@ -0,0 +1,90 @@ +#include +#include + +using namespace std; +int N, M; +int board[50][50]; +int room[50][50]; +int c[2500]; +bool visited[50][50]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int w[4] = {4, 1, 8, 2}; +int ans2 = 0; + +int dfs(int i, int j, int n) +{ + visited[i][j] = true; + room[i][j] = n; + + int res = 1; + + for (int x = 0; x < 4; x++) + { + int ni = i + dx[x]; + int nj = j + dy[x]; + if ((board[i][j] & w[x]) == 0) + { + if (ni >= 0 && nj >= 0 && ni < N && nj < M && !visited[ni][nj]) + { + res += dfs(ni, nj, n); + } + } + } + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> M >> N; + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> board[i][j]; + } + } + + int ans1 = 0; + + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + if (!visited[i][j]) + { + c[ans1] = dfs(i, j, ans1); + ans2 = max(c[ans1], ans2); + ans1++; + } + } + } + + int ans3 = c[0]; + + for (int dir = 0; dir < 4; dir++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + int nx = i + dx[dir]; + int ny = j + dy[dir]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && (board[i][j] & w[dir]) == w[dir] && room[i][j] != room[nx][ny]) + { + ans3 = max(ans3, c[room[i][j]] + c[room[nx][ny]]); + } + } + } + } + + cout << ans1 << "\n" + << ans2 << "\n" + << ans3 << "\n"; + + return 0; +} diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" new file mode 100644 index 0000000..3fab2c0 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" @@ -0,0 +1,57 @@ +#include + +using namespace std; +int T, N; +int dp_table[30][6]; + +int dp(int n, int status) +{ + int res; + if (dp_table[n][status] != 0) + return dp_table[n][status]; + + if (n == 0 && status == 1) + return 1; + else if (n == 0) + return 0; + if (n == 1) + { + if (status == 4) + return 0; + return 1; + } + + switch (status) + { + case 1: + res = dp(n - 1, 1) + dp(n - 1, 2) + dp(n - 1, 3) + dp(n - 1, 5) + dp(n - 2, 1); + break; + case 2: + res = dp(n - 1, 1) + dp(n - 1, 3); + break; + case 3: + res = dp(n - 1, 1) + dp(n - 1, 2); + break; + case 4: + res = dp(n - 1, 5); + break; + case 5: + res = dp(n - 1, 1) + dp(n - 1, 4); + break; + } + return dp_table[n][status] = res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + int i; + for (i = 0; i < T; i++) + { + cin >> N; + cout << dp(N, 1) << "\n"; + } + return 0; +} diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" new file mode 100644 index 0000000..6c58c4d --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +int N, M, K; + +int seg[4000000]; +int A[1000001]; + +int init(int node, int start, int end) +{ + if (start > N) + return 1; + if (start == end) + return seg[node] = A[start]; + int middle = (start + end) / 2; + return seg[node] = + (long long)init(node * 2, start, middle) * init(node * 2 + 1, middle + 1, end) % 1000000007; +} + +int find(int node, int left, int right, int start, int end) +{ + if (start >= left && end <= right) + return seg[node]; + if (start > right || end < left) + return 1; + + int middle = (start + end) / 2; + + return (long long)find(node * 2, left, right, start, middle) * find(node * 2 + 1, left, right, middle + 1, end) % 1000000007; +} + +int rebuild(int node, int start, int end, int index, int n) +{ + if (start > N || index < start || end < index) + return seg[node]; + + if (start == end && index == start) + return seg[node] = n; + + int middle = (start + end) / 2; + return seg[node] = (long long)rebuild(node * 2, start, middle, index, n) * rebuild(node * 2 + 1, middle + 1, end, index, n) % 1000000007; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> K; + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + int x, y, z; + + for (int i = 0; i < M + K; i++) + { + cin >> x >> y >> z; + if (x == 1) + { + rebuild(1, 1, pow(2, ceil(log2(N))), y, z); + } + else + { + cout << find(1, y, z, 1, pow(2, ceil(log2(N)))) << "\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" new file mode 100644 index 0000000..7ab2e25 --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" @@ -0,0 +1,98 @@ +#include +#include + +using namespace std; + +int N; +long long A[100001]; +int seg[400000]; +long long sum[100001]; + +int init(int node, int start, int end) +{ + if (start > N) + return -1; + if (start == end) + return seg[node] = start; + + int middle = (start + end) / 2; + + int lefti = init(node * 2, start, middle); + int righti = init(node * 2 + 1, middle + 1, end); + if (lefti == -1) + seg[node] = righti; + else if (righti == -1) + seg[node] = lefti; + else if (A[lefti] < A[righti]) + seg[node] = lefti; + else + seg[node] = righti; + return seg[node]; +} + +int get_min_index(int node, int left, int right, int start, int end) +{ + + if (left > end || right < start) + return -1; + + if (left <= start && right >= end) + return seg[node]; + + int middle = (start + end) / 2; + + int lefti = get_min_index(node * 2, left, right, start, middle); + int righti = get_min_index(node * 2 + 1, left, right, middle + 1, end); + + if (lefti == -1) + return righti; + if (righti == -1) + return lefti; + if (A[lefti] < A[righti]) + return lefti; + return righti; +} + +long long solve(int left, int right) +{ + int index = get_min_index(1, left, right, 1, pow(2, ceil(log2(N)))); + long long ans = sum[right] - sum[left - 1]; + ans = ans * A[index]; + + if (index - 1 >= left) + { + ans = max(ans, solve(left, index - 1)); + } + if (index + 1 <= right) + { + ans = max(ans, solve(index + 1, right)); + } + + //cout << left << " " << right << " " << ans << "\n"; + return ans; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + for (i = 1; i <= N; i++) + { + cin >> A[i]; + } + + for (i = 1; i <= N; i++) + { + sum[i] = sum[i - 1] + A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + + cout << solve(1, N) << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" new file mode 100644 index 0000000..0254d9b --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" @@ -0,0 +1,95 @@ +#include +#include + +using namespace std; + +int N, K; +int A[100001]; +int seg[400002]; + +int get101(int n) +{ + if (n > 0) + return 1; + else if (n < 0) + return -1; + return 0; +} + +int init(int node, int left, int right) +{ + if (left > N) + return 1; + if (left == right) + return seg[node] = get101(A[left]); + + int middle = (left + right) / 2; + return seg[node] = init(node * 2, left, middle) * init(node * 2 + 1, middle + 1, right); +} + +int change(int node, int left, int right, int index, int n) +{ + if (left > index || right < index) + return seg[node]; + + if (left == right) + { + return seg[node] = get101(n); + } + + int middle = (left + right) / 2; + return seg[node] = change(node * 2, left, middle, index, n) * change(node * 2 + 1, middle + 1, right, index, n); +} + +int getseg(int node, int left, int right, int start, int end) +{ + if (left > end || right < start) + return 1; + if (left >= start && right <= end) + return seg[node]; + int middle = (left + right) / 2; + return getseg(node * 2, left, middle, start, end) * getseg(node * 2 + 1, middle + 1, right, start, end); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int x, y; + + while (cin >> N >> K) + { + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + + for (int i = 0; i < K; i++) + { + char c; + cin >> c; + if (c == 'C') + { + cin >> x >> y; + change(1, 1, pow(2, ceil(log2(N))), x, y); + } + else + { + cin >> x >> y; + int a = getseg(1, 1, pow(2, ceil(log2(N))), x, y); + if (a == 1) + cout << "+"; + else if (a == 0) + cout << "0"; + else + cout << "-"; + } + } + + cout << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/1662.cpp" "b/baekjoon/\354\212\244\355\203\235/1662.cpp" new file mode 100644 index 0000000..ae4f479 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/1662.cpp" @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '(') + { + stack.push_back({-1, 0}); + } + else if (s[i] == ')') + { + + int tmp = 0; + while (stack.back().first != -1) + { + tmp += stack.back().first; + stack.pop_back(); + } + stack.pop_back(); + tmp *= stack.back().second; + stack.pop_back(); + stack.push_back({tmp, tmp}); + } + else + { + stack.push_back({1, s[i] - '0'}); + } + } + + int ans = 0; + while (!stack.empty()) + { + ans += stack.back().first; + stack.pop_back(); + } + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/17178.cpp" "b/baekjoon/\354\212\244\355\203\235/17178.cpp" new file mode 100644 index 0000000..e25dd56 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/17178.cpp" @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; +list< pair > q[100]; +list< pair > total; +vector< pair > waiting; + +int N; + +pair string_strip(string s){ + pair res; + res.first = s[0] - 'A'; + int num = 0; + for(unsigned int i=2; i= '0' && s[i] <= '9'; i++){ + num = num*10 + s[i] - '0'; + } + res.second = num; + return res; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + string tmp; + + cin >> N; + for(int i=0; i> tmp; + q[i].push_back(string_strip(tmp)); + total.push_back(string_strip(tmp)); + } + } + + total.sort(); + + bool flag = true; + + while(!total.empty() && flag){ + + // cout << (char)(total.front().first + 'A') << " " << total.front().second << "\n"; + // for(int i=0; i biggest = make_pair(0, 0); + + if(!waiting.empty() && waiting.back() == total.front()){ + waiting.pop_back(); + total.pop_front(); + flag = true; + continue; + } + + for(int i=0; i +#include + +using namespace std; + +int N; +int A[1000000]; +int ans[1000000]; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + for (i = N - 1; i >= 0; i--) + { + while (!stack.empty() && stack.back() <= A[i]) + { + stack.pop_back(); + } + + if (stack.empty()) + { + ans[i] = -1; + stack.push_back(A[i]); + } + else + { + ans[i] = stack.back(); + stack.push_back(A[i]); + } + } + + for (i = 0; i < N; i++) + { + cout << ans[i] << " "; + } + cout << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/17299.cpp" "b/baekjoon/\354\212\244\355\203\235/17299.cpp" new file mode 100644 index 0000000..3ac8313 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/17299.cpp" @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int N; +int A[1000000]; +int F[1000001]; +int ans[1000000]; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + F[A[i]]++; + } + + for (i = N - 1; i >= 0; i--) + { + + while (!stack.empty() && F[stack.back()] <= F[A[i]]) + { + stack.pop_back(); + } + + if (stack.empty()) + { + ans[i] = -1; + stack.push_back(A[i]); + } + else + { + ans[i] = stack.back(); + stack.push_back(A[i]); + } + } + + for (i = 0; i < N; i++) + { + cout << ans[i] << " "; + } + cout << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/2493.cpp" "b/baekjoon/\354\212\244\355\203\235/2493.cpp" new file mode 100644 index 0000000..0cfcd62 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/2493.cpp" @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +int N; +vector > stack; +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i, x; + for (i = 0; i < N; i++) + { + cin >> x; + if (stack.empty()) + { + cout << "0 "; + stack.push_back({i + 1, x}); + } + else if (stack.back().second < x) + { + while (!stack.empty() && stack.back().second < x) + { + stack.pop_back(); + } + if (stack.empty()) + { + cout << "0 "; + } + else + { + cout << stack.back().first << " "; + } + stack.push_back({i + 1, x}); + } + else + { + cout << stack.back().first << " "; + stack.push_back({i + 1, x}); + } + } + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/3015.cpp" "b/baekjoon/\354\212\244\355\203\235/3015.cpp" new file mode 100644 index 0000000..0e0dc6a --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/3015.cpp" @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; +typedef long long ll; +vector stack; +int N; + +int getlb(int left, int right, ll key) +{ + if (left >= right) + return left; + + int middle = (left + right) / 2 + 1; + if (stack[middle] <= key) + { + return getlb(left, middle - 1, key); + } + else + { + return getlb(middle, right, key); + } +} + +int getSize(ll x) +{ + int index = getlb(0, stack.size() - 1, x); + return stack.size() - index; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i; + ll x, ans = 0; + + for (i = 0; i < N; i++) + { + cin >> x; + if (stack.empty()) + { + stack.push_back(x); + } + else if (stack.back() > x) + { + ans += 1; + stack.push_back(x); + } + else + { + ans += getSize(x); + while (!stack.empty() && stack.back() < x) + { + stack.pop_back(); + } + stack.push_back(x); + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/6198.cpp" "b/baekjoon/\354\212\244\355\203\235/6198.cpp" new file mode 100644 index 0000000..83806b9 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/6198.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +int N; +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + long long ans = 0; + + for (int i = 0; i < N; i++) + { + int x; + cin >> x; + if (stack.empty()) + { + stack.push_back(make_pair(x, 0)); + } + else if (stack.back().first > x) + { + stack.push_back(make_pair(x, 0)); + } + else + { + while(!stack.empty() && stack.back().first <= x){ + pair d = stack.back(); + ans += d.second; + stack.pop_back(); + if(!stack.empty()){ + stack.back().second += d.second + 1; + } + } + stack.push_back(make_pair(x, 0)); + } + } + + while(!stack.empty()){ + pair d = stack.back(); + ans += d.second; + stack.pop_back(); + if(!stack.empty()){ + stack.back().second += d.second + 1; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\354\212\244\355\203\235/9935.cpp" "b/baekjoon/\354\212\244\355\203\235/9935.cpp" new file mode 100644 index 0000000..c4d1b7b --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/9935.cpp" @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string s; + string target; + + cin >> s >> target; + + int i; + for (i = 0; i < s.length(); i++) + { + if (stack.empty()) + { + int tmp = target[0] == s[i] ? 0 : -1; + stack.push_back({s[i], tmp}); + } + else + { + int checkIndex = stack.back().second + 1; + int tmp; + if (target[checkIndex] == s[i]) + tmp = checkIndex; + else if (target[0] == s[i]) + tmp = 0; + else + tmp = -1; + + stack.push_back({s[i], tmp}); + } + + if (stack.back().second == target.length() - 1) + { + int count = 0; + while (count < target.length()) + { + stack.pop_back(); + count++; + } + } + } + + if(stack.empty()){ + cout << "FRULA\n"; + } + else{ + for(pair a : stack){ + cout << a.first; + } + cout <<"\n"; + } + return 0; +} diff --git "a/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" "b/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" new file mode 100644 index 0000000..e27ea22 --- /dev/null +++ "b/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +int N; +int P[11]; +vector edges[11]; +int ans = 10000; +bool visited[11]; +int check[12]; + +void dfs(int n, int k){ + visited[n] = true; + for(int next:edges[n]){ + if(!visited[next] && check[next]==k){ + dfs(next, k); + } + } +} + +void solve(int n){ + if(n==N+1){ + for(int i=1; i<=N; i++) visited[i] = false; + for(int i=1; i<=N; i++){ + if(check[i] == 0){ + dfs(i, 0); + break; + } + } + for(int i=1; i<=N; i++){ + if(check[i] == 11){ + dfs(i, 11); + break; + } + } + for(int i=1; i<=N; i++){ + if(!visited[i]) return; + } + + int p1=0, p2=0; + for(int i=1; i<=N; i++){ + if(check[i] == 0) p1+=P[i]; + else p2 += P[i]; + } + if(p1==0 || p2==0) return; + ans = min(ans, abs(p1-p2)); + return; + } + + check[n] = 0; + solve(n+1); + check[n] = 11; + solve(n+1); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=1; i<=N; i++){ + cin >> P[i]; + } + + int x, tmp; + for(int i=1; i<=N; i++){ + cin >> x; + for(int j=0; j> tmp; + edges[i].push_back(tmp); + } + } + + solve(1); + + if(ans == 10000) cout << "-1\n"; + else cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" new file mode 100644 index 0000000..35475aa --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +struct Jew +{ + long long m, v; + + bool operator<(const Jew &a) const + { + if (v == a.v) + return m > a.m; + return v < a.v; + } +}; + +priority_queue jews; +long long ans; +multiset back; +int N, K; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + long long x, y, i; + for (i = 0; i < N; i++) + { + cin >> x >> y; + jews.push({x, y}); + } + for (i = 0; i < K; i++) + { + cin >> x; + back.insert(x); + } + + while (!jews.empty()) + { + long long c = jews.top().v; + long long w = jews.top().m; + jews.pop(); + + multiset::iterator it = back.lower_bound(w); + //cout << *it << " "; + if (it != back.end()) + { + back.erase(it); + ans += c; + } + } + + cout << ans << "\n"; + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" new file mode 100644 index 0000000..6d596df --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" @@ -0,0 +1,102 @@ +#include +#include + +using namespace std; + +int N, M; + +struct node +{ + int x, y, through, by; + + bool operator<(const node &a) const + { + if (through == a.through) + return by > a.by; + return through > a.through; + } +}; + +priority_queue pq; +string board[50]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +bool visited[50][50]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + for (j = 0; j < M; j++) + { + if (board[i][j] == 'S') + { + pq.push({i, j, 0, 0}); + } + } + } + + while (!pq.empty()) + { + int x = pq.top().x; + int y = pq.top().y; + int through = pq.top().through; + int by = pq.top().by; + pq.pop(); + + if (board[x][y] == 'F') + { + cout << through << " " << by << "\n"; + break; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !visited[nx][ny]) + { + if (board[nx][ny] == 'g') + { + pq.push({nx, ny, through + 1, by}); + visited[nx][ny] = true; + } + else if (board[nx][ny] == '.') + { + bool trash = false; + for (j = 0; j < 4; j++) + { + int cx = nx + dx[j]; + int cy = ny + dy[j]; + if (cx >= 0 && cy >= 0 && cx < N && cy < M && board[cx][cy] == 'g') + { + trash = true; + break; + } + } + if (trash) + { + pq.push({nx, ny, through, by + 1}); + } + else + { + pq.push({nx, ny, through, by}); + } + visited[nx][ny] = true; + } + else + { + pq.push({nx, ny, through, by}); + visited[nx][ny] = true; + } + } + } + } + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" new file mode 100644 index 0000000..7e6c3b9 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +typedef long long ll; + +int T, N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + int i, j; + for (i = 0; i < T; i++) + { + ll x, ans = 1; + cin >> N; + priority_queue pq; + for (j = 0; j < N; j++) + { + cin >> x; + pq.push(-x); + } + while (pq.size() > 1) + { + ll tmp = pq.top(); + pq.pop(); + tmp *= pq.top(); + pq.pop(); + //cout << tmp << " "; + pq.push(-tmp); + ans = (ans * (tmp % 1000000007)) % 1000000007; + } + cout << ans << "\n"; + } + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" new file mode 100644 index 0000000..67cbcb2 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" @@ -0,0 +1,83 @@ +#include +#include + +using namespace std; + +vector stack; + +char target[4] = {'P', 'P', 'A', 'P'}; + +void print() +{ + for (int s : stack) + { + cout << s << " "; + } + cout << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + + if (s[i] == 'P') + { + if (!stack.empty() && stack.back() == 1) + { + stack.push_back(2); + } + else + stack.push_back(0); + } + else + { + stack.push_back(1); + } + + while (stack.back() == 2) + { + stack.pop_back(); + stack.pop_back(); + if (!stack.empty() && stack.back() != 1) + { + stack.pop_back(); + if (!stack.empty() && stack.back() != 1) + { + stack.pop_back(); + } + else + { + cout << "NP\n"; + return 0; + } + } + else + { + cout << "NP\n"; + return 0; + } + + if (!stack.empty() && stack.back() == 1) + stack.push_back(2); + else + stack.push_back(0); + } + } + + if (!stack.empty() && (stack.back() == 1 || stack.size() > 1)) + { + cout << "NP\n"; + } + else + cout << "PPAP\n"; + + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" new file mode 100644 index 0000000..b6086e4 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + +struct out +{ + int n, number, time; + + bool operator<(const out &a) const + { + if (time == a.time) + return n < a.n; + return time > a.time; + } +}; + +struct in +{ + int n, number, time; + + bool operator<(const in &a) const + { + if (time == a.time) + return n > a.n; + return time > a.time; + } +}; + +priority_queue O; +priority_queue I; +int N, K; +long long ans = 0; +int totaltime = 0; +int cus[100000][2]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + + int i, j, x, y; + for (i = 1; i <= K; i++) + { + I.push({i, 0, 0}); + } + + for (i = 0; i < N; i++) + { + cin >> cus[i][0] >> cus[i][1]; + } + + int current = 0; + int out = 1; + while (current < N) + { + if (I.top().time <= totaltime) + { + struct in t = I.top(); + I.pop(); + t.number = cus[current][0]; + t.time += cus[current][1]; + I.push(t); + O.push({t.n, t.number, t.time}); + cout << "in " << t.n << " " << current << " " << totaltime << "\n"; + current++; + } + else + { + totaltime++; + } + } + + while (!O.empty()) + { + ans += (long long)O.top().number * out; + O.pop(); + out++; + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" new file mode 100644 index 0000000..c254962 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + +typedef long long ll; +priority_queue pq; +map visited; +int K, N; +ll prime[100]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> K >> N; + + int i, j; + for (i = 0; i < K; i++) + { + cin >> prime[i]; + } + + pq.push(-1); + + ll maxValue = 1; + + while (N > 0) + { + ll cur = -pq.top(); + pq.pop(); + + for (i = 0; i < K; i++) + { + ll element = cur * prime[i]; + + if (pq.size() > N && maxValue < element) + continue; + + if (!visited.count(element)) + { + maxValue = max(element, maxValue); + pq.push(-element); + visited[element] = true; + } + } + N--; + } + + cout << -pq.top() << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" new file mode 100644 index 0000000..a9e02e3 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +priority_queue pq; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i, j, x; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + cin >> x; + if (pq.size() < N) + pq.push(-x); + else if (-pq.top() < x) + { + pq.pop(); + pq.push(-x); + } + } + } + + cout << -pq.top() << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" new file mode 100644 index 0000000..ea1e96e --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; +int T, N; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0;t> N; + priority_queue maxHeap; + priority_queue minHeap; + vector ans; + + int x; + + for(int i=1;i<=N;i++){ + cin >> x; + if(maxHeap.empty()){ + maxHeap.push(x); + } + else if(maxHeap.size()>minHeap.size()){ + int middle = maxHeap.top(); + if(x < middle){ + maxHeap.pop(); + maxHeap.push(x); + minHeap.push(-middle); + } + else{ + minHeap.push(-x); + } + } + else{ + int maxmiddle = maxHeap.top(); + int minmiddle = -minHeap.top(); + + if(x < minmiddle){ + maxHeap.push(x); + }else{ + minHeap.pop(); + maxHeap.push(minmiddle); + minHeap.push(-x); + } + } + if(i%2==1) ans.push_back(maxHeap.top()); + } + cout << ans.size() << "\n"; + for(int a :ans){ + cout << a << " "; + } + cout << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" new file mode 100644 index 0000000..6697ac0 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" @@ -0,0 +1,65 @@ +#include +#include +#include + +using namespace std; + +int N, M; +int degree[1001]; +int ans[1001]; +vector edges[1001]; +queue q; +bool visited[1001]; + +int main() +{ + ios::sync_with_stdio(0); + + cin >> N >> M; + + int x, y; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + edges[x].push_back(y); + degree[y]++; + } + + int time = 1; + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + { + q.push(i); + visited[i] = true; + ans[i] = time; + } + } + + while (!q.empty()) + { + x = q.front(); + q.pop(); + + for (int v : edges[x]) + { + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + visited[v] = true; + q.push(v); + ans[v] = ans[x] + 1; + } + } + } + } + + for (int i = 1; i <= N; i++) + { + cout << ans[i] << " "; + } + + return 0; +} diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" new file mode 100644 index 0000000..482306a --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +int N, M; +priority_queue pq; +vector edges[32001]; +int degree[32001]; +bool visited[32001]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N >> M; + int x, y; + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + edges[x].push_back(y); + degree[y]++; + } + + int selected = 0; + + for (int i = 1; i <= N; i++) + { + if (!visited[i] && degree[i] == 0) + { + pq.push(-i); + visited[i] = true; + } + } + + while (selected < N) + { + int t = -pq.top(); + pq.pop(); + + cout << t << " "; + selected++; + + for (int v : edges[t]) + { + degree[v]--; + if (!visited[v] && degree[v] == 0) + { + pq.push(-v); + visited[v] = true; + } + } + } + + return 0; +} diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" new file mode 100644 index 0000000..0cbd65e --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int N; +vector v[10001]; +priority_queue< pair > q; +int t[10001]; +int d[10001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + for(int i=1; i<=N; i++){ + cin >> t[i]; + int x, y; + cin >> x; + for(int j=0; j> y; + v[y].push_back(i); + d[i]++; + } + } + + for(int i=1; i<=N; i++){ + if(d[i] == 0){ + q.push(make_pair(-t[i], i)); + } + } + + int ans = 0; + + while(!q.empty()){ + int x = q.top().second; + int y = -q.top().first; + q.pop(); + ans = max(ans, y); + + for(int k:v[x]){ + d[k]--; + if(d[k]==0){ + q.push(make_pair(-y-t[k], k)); + } + } + } + + cout << ans << "\n"; + + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" new file mode 100644 index 0000000..ec672af --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" @@ -0,0 +1,78 @@ +#include +#include +#include + +using namespace std; + +int N, M; +vector edges[1001]; +int degree[1001]; +queue q; +bool visited[1001]; +vector ans; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int x, y, z; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + for (int j = 0; j < x - 1; j++) + { + cin >> z; + edges[y].push_back(z); + degree[z]++; + y = z; + } + } + + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + { + q.push(i); + visited[i] = true; + } + } + + int selected = 0; + while (!q.empty()) + { + x = q.front(); + q.pop(); + + selected += 1; + ans.push_back(x); + + for (int v : edges[x]) + { + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + q.push(v); + visited[v] = true; + } + } + } + } + + if (selected < N) + { + cout << "0\n"; + } + else + { + for (int a : ans) + { + cout << a << "\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" new file mode 100644 index 0000000..0b448f2 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int degree[101]; +vector > edges[101]; +queue q; +bool visited[101]; +int ans[101]; +int N, M; + +int main() +{ + ios::sync_with_stdio(0); + + cin >> N >> M; + int x, y, z; + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + degree[y]++; + edges[x].push_back({z, y}); + } + + ans[N] = 1; + visited[N] = true; + q.push(N); + + while (!q.empty()) + { + x = q.front(); + q.pop(); + + if (edges[x].empty()) + continue; + + for (pair vertex : edges[x]) + { + int c = vertex.first; + int v = vertex.second; + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + visited[v] = true; + q.push(v); + } + ans[v] += ans[x] * c; + } + } + + ans[x] = 0; + } + + for (int i = 1; i <= N; i++) + { + if (ans[i] != 0) + { + cout << i << " " << ans[i] << "\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" new file mode 100644 index 0000000..847e4e5 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" @@ -0,0 +1,116 @@ +#include +#include +#include + +using namespace std; + +int T, N, S; +int A[501]; +int degree[501]; +bool edge[501][501]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N; + for (int i = 0; i < N; i++) + { + cin >> A[i]; + } + + for (int i = 0; i <= N; i++) + { + degree[i] = 0; + for (int j = 0; j <= N; j++) + { + edge[i][j] = false; + } + } + + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + degree[A[j]]++; + edge[A[i]][A[j]] = true; + } + } + cin >> S; + int x, y; + for (int i = 0; i < S; i++) + { + cin >> x >> y; + if (edge[x][y]) + { + degree[y]--; + degree[x]++; + edge[x][y] = false; + edge[y][x] = true; + } + else if (edge[y][x]) + { + degree[x]--; + degree[y]++; + edge[y][x] = false; + edge[x][y] = true; + } + } + + queue q; + vector ans; + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + q.push(i); + } + + while (!q.empty()) + { + if (q.size() >= 2) + { + break; + } + + x = q.front(); + q.pop(); + ans.push_back(x); + + for (int i = 1; i <= N; i++) + { + if (edge[x][i]) + { + edge[x][i] = false; + degree[i]--; + if (degree[i] == 0) + { + q.push(i); + } + } + } + } + + if (ans.size() == N) + { + for (int a : ans) + { + cout << a << " "; + } + cout << "\n"; + } + else if (q.size() >= 2) + { + cout << "?\n"; + } + else + { + cout << "IMPOSSIBLE\n"; + } + } + return 0; +} diff --git "a/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" "b/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" new file mode 100644 index 0000000..c538ded --- /dev/null +++ "b/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int N, K; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + + int left = 1, right = 1000000000; + int middle; + int ans; + + while(left <= right){ + middle = (left+right)/2; + + int count = 0; + for(int i=1; i<=N; i++){ + count += min(N, middle/i); + } + //cout << middle << " " << count << "\n"; + if(count >= K){ + ans = middle; + right = middle-1; + } + else{ + left = middle+1; + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" new file mode 100644 index 0000000..38fb411 --- /dev/null +++ "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; + +struct node{ + int x, y; + + bool operator< (const node & a) const{ + if(y==a.y) return x > a.x; + return y > a.y; + } +}; + +int N, M; +priority_queue pq; +priority_queue include; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x >> y; + if(x<=y){ + pq.push({x, y}); + } + else{ + pq.push({y, x}); + } + } + cin >> M; + + int start = -1000000001; + int end = -1000000001; + int ans = 0; + + while(!pq.empty()){ + int x = pq.top().x; + int y = pq.top().y; + pq.pop(); + + cout << "[" << x << " " << y << "]\n"; + + if(y-x > M) continue; + + if(y<=end) include.push(-x); + else{ + end = y; + start = y-M; + while(!include.empty()){ + int k = -include.top(); + if(k + +using namespace std; + +int N; + +struct node{ + int n; + int t; + struct node* previous; + struct node* next; +}; + +struct node list[1000]; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x; + list[i].t = x; + } + + struct node* now = &list[0]; + for(int i=0; in << " "; + now->previous->next = now->next; + now->next->previous = now->previous; + int k = now->t; + if(k>0){ + for(int j=0; jnext; + } + } + else{ + for(int j=0; j>k; j--){ + now = now->previous; + } + } + } + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" new file mode 100644 index 0000000..c49ecac --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" @@ -0,0 +1,96 @@ +#include +#include + +using namespace std; + +int N, T; + +vector edges[50001]; +int depth[50001]; +bool visited[50001]; +int lca[50001][16]; + +void dfs(int n, int d, int pre) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = pre; + + for (int v : edges[n]) + { + if (!visited[v]) + { + dfs(v, d + 1, n); + } + } +} + +void buildlca() +{ + for (int j = 1; j < 16; j++) + { + for (int i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + } + } +} + +int getlca(int x, int y) +{ + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 15; i >= 0; i--) + { + if (depth[y] - depth[x] >= (1 << i)) + { + y = lca[y][i]; + } + } + + if (x == y) + return x; + + for (int i = 15; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + x = lca[x][i]; + y = lca[y][i]; + } + } + return lca[x][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y, i; + + for (i = 0; i < N - 1; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + dfs(1, 1, 0); + buildlca(); + + cin >> T; + + for (i = 0; i < T; i++) + { + cin >> x >> y; + cout << getlca(x, y) << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" new file mode 100644 index 0000000..fe349f9 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" @@ -0,0 +1,106 @@ +#include +#include + +using namespace std; + +int N, M; + +vector edges[100001]; +bool visited[100001]; +int depth[100001]; +int lca[100001][18]; + +void dfs(int n, int d, int p) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = p; + + for (int v : edges[n]) + { + if (!visited[v]) + { + dfs(v, d + 1, n); + } + } +} + +void build_lca() +{ + for (int j = 1; j < 18; j++) + { + for (int i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + } + } +} + +int find_lca(int x, int y) +{ + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 17; i >= 0; i--) + { + if (depth[y] >= depth[x] + (1 << i)) + { + y = lca[y][i]; + } + } + + if (x == y) + return x; + + for (int i = 17; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + x = lca[x][i]; + y = lca[y][i]; + } + } + return lca[x][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + + int i; + int x, y, z; + for (i = 0; i < N - 1; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + dfs(1, 0, 0); + build_lca(); + + cin >> M; + for (i = 0; i < M; i++) + { + cin >> x >> y >> z; + + int lca1 = find_lca(x, y); + int lca2 = find_lca(x, z); + int lca3 = find_lca(y, z); + + int ans = lca1; + if (depth[lca2] > depth[ans]) + ans = lca2; + if (depth[lca3] > depth[ans]) + ans = lca3; + + cout << ans << "\n"; + } + return 0; +} diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" new file mode 100644 index 0000000..29ebdb4 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" @@ -0,0 +1,103 @@ +#include +#include + +using namespace std; + +int N, M; +int depth[40001]; +vector > edges[40001]; +int lca[40001][18]; +int dist[40001][18]; +bool visited[40001]; + +void dfs(int n, int d, int pre, int predist) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = pre; + dist[n][0] = predist; + + for (pair v : edges[n]) + { + if (!visited[v.second]) + { + dfs(v.second, d + 1, n, v.first); + } + } +} + +void build_lca() +{ + int i, j; + for (j = 1; j < 18; j++) + { + for (i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + dist[i][j] = dist[i][j - 1] + dist[lca[i][j - 1]][j - 1]; + } + } +} + +int getlca(int x, int y) +{ + int ans = 0; + + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 17; i >= 0; i--) + { + if (depth[y] - depth[x] >= (1 << i)) + { + ans += dist[y][i]; + y = lca[y][i]; + } + } + + if (x == y) + return ans; + + for (int i = 17; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + ans += dist[x][i] + dist[y][i]; + x = lca[x][i]; + y = lca[y][i]; + } + } + return ans + dist[x][0] + dist[y][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y, z, i; + + for (i = 0; i < N - 1; i++) + { + cin >> x >> y >> z; + edges[x].push_back({z, y}); + edges[y].push_back({z, x}); + } + + dfs(1, 1, 0, 0); + build_lca(); + + cin >> M; + + for (i = 0; i < M; i++) + { + cin >> x >> y; + cout << getlca(x, y) << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" new file mode 100644 index 0000000..f044f6c --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; + +int p[1001]; +int selected = 0; +int ans = 0; + +priority_queue > > pq; + +int find(int n) +{ + if (p[n] == n) + return n; + return p[n] = find(p[n]); +} + +void Union(int x, int y) +{ + if (x > y) + swap(x, y); + + p[y] = x; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M, K; + cin >> N >> M >> K; + + for (int i = 0; i <= N; i++) + { + p[i] = i; + } + + int x, y, z; + for (int i = 0; i < K; i++) + { + cin >> x; + p[x] = 0; + if (i != K - 1) + selected++; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + pq.push(make_pair(-z, make_pair(x, y))); + } + + while (selected < N - 1) + { + int w = -pq.top().first; + x = pq.top().second.first; + y = pq.top().second.second; + pq.pop(); + + if (find(x) != find(y)) + { + Union(p[x], p[y]); + selected++; + ans += w; + } + } + + cout << ans << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" new file mode 100644 index 0000000..3b3918f --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace std; + +int p1[1001]; +int p2[1001]; +int selected1 = 0, selected2 = 0, N, M; +long long total1 = 0, total2 = 0; +pair > edges[1000001]; + +int getp1(int n){ + if(p1[n] == n) return n; + return p1[n] = getp1(p1[n]); +} + +int getp2(int n){ + if(p2[n] == n) return n; + return p2[n] = getp2(p2[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i<=N; i++) { + p1[i] = i; + p2[i] = i; + } + + for(int i=0; i> x >> y >> z; + edges[i] = make_pair(z, make_pair(x,y)); + } + + sort(edges, edges + M + 1); + + for(int now=M; selected1 < N; now--){ + + int d = edges[now].first; + int x = edges[now].second.first; + int y = edges[now].second.second; + + if(getp1(x) != getp1(y)){ + p1[p1[x]] = p1[y]; + if(d==0) total1++; + selected1++; + } + } + + for(int now=0; selected2 < N; now++){ + + int d = edges[now].first; + int x = edges[now].second.first; + int y = edges[now].second.second; + + if(getp2(x) != getp2(y)){ + p2[p2[x]] = p2[y]; + if(d==0) total2++; + selected2++; + } + } + + cout << -total1*total1 + total2*total2 << "\n"; + + return 0; +} diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" new file mode 100644 index 0000000..a48966a --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +int N, M, T; +priority_queue< pair > > pq; +int p[10001]; + +int getp(int n){ + if(p[n] == n) return n; + return p[n] = getp(p[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> T; + + for(int i=1; i<=N; i++){ + p[i] = i; + } + + for(int i=0; i> x >> y >> z; + pq.push(make_pair(-z, make_pair(x,y))); + } + + int selected = 0; + int offset = 0; + long long ans = 0; + while(!pq.empty() && selected < N-1){ + int c = -pq.top().first; + int x = pq.top().second.first; + int y = pq.top().second.second; + pq.pop(); + + if(getp(x) != getp(y)){ + p[p[x]] = p[y]; + ans += c + offset; + offset += T; + selected++; + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" new file mode 100644 index 0000000..087bb56 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + +int N, M; +int p[1001]; +priority_queue< pair > > pq; +int cost[1001][1001]; +vector< pair > ans; + +int getp(int n){ + if(n == p[n]) return n; + return p[n] = getp(p[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + + for(int i=1; i<=N; i++) p[i] = i; + + int selected = 0; + + for(int i=0; i> x >> y; + if(getp(x) != getp(y)){ + selected++; + p[p[x]] = p[y]; + } + } + + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + cin >> cost[i][j]; + if(i!=1 && i a:ans){ + cout << a.first << " " << a.second << "\n"; + } + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" new file mode 100644 index 0000000..d32a748 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +int p[200001]; + +struct node +{ + long long w; + int x, y; + + bool operator<(const node &a) const + { + return w > a.w; + } +}; + +int find(int n) +{ + if (n == p[n]) + return n; + return p[n] = find(p[n]); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M, x, y; + long long z; + + while (true) + { + cin >> N >> M; + if (N == 0 && M == 0) + { + break; + } + + for (int i = 0; i < N; i++) + { + p[i] = i; + } + + long long total = 0; + priority_queue pq; + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + total += z; + node new_node; + new_node.w = z; + new_node.x = x; + new_node.y = y; + pq.push(new_node); + } + + int selected = 0; + long long select = 0; + + while (selected < N - 1) + { + node top = pq.top(); + pq.pop(); + + if (find(top.x) != find(top.y)) + { + select += top.w; + selected++; + p[p[top.x]] = p[top.y]; + } + } + + cout << total - select << "\n"; + } + + return 0; +} diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" new file mode 100644 index 0000000..c35df91 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +int N; + +class Node{ + public: + string str; + map children; + + Node(string s){ + str = s; + } +}; + +Node* trie = new Node("root"); + +void dfs(Node* now, int level){ + if(now->children.empty()) return; + map::iterator it; + for(it=now->children.begin(); it!=now->children.end(); it++){ + for(int i=0; ifirst << "\n"; + dfs(it->second, level+1); + } +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x; + string tmp; + + for(int i=0; i> x; + Node* now = trie; + for(int j=0; j> tmp; + if(now->children.find(tmp)==now->children.end()){ + now->children[tmp] = new Node(tmp); + } + now = now->children[tmp]; + } + } + + dfs(trie, 0); + + return 0; +} \ No newline at end of file diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" new file mode 100644 index 0000000..a4c47dd --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" @@ -0,0 +1,77 @@ +#include + +using namespace std; + +class Tree +{ +public: + Tree *next[26] = {NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL}; + int count = 0; +}; + +class TrieTree +{ +public: + Tree *root = new Tree(); + + void insert(string str) + { + bool first = true; + string ans = ""; + Tree *current = root; + for (int i = 0; i < str.length(); i++) + { + if (current->next[str[i] - 'a'] == NULL) + { + current->next[str[i] - 'a'] = new Tree(); + if (first) + { + ans = str.substr(0, i + 1); + first = false; + } + } + current = current->next[str[i] - 'a']; + } + + current->count++; + + if (first) + { + if (current->count == 1) + { + cout << str << "\n"; + } + else + { + cout << str << current->count << "\n"; + } + } + else + { + cout << ans << "\n"; + } + } +}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N; + string tmp; + TrieTree trieTree; + + cin >> N; + for (int i = 0; i < N; i++) + { + cin >> tmp; + trieTree.insert(tmp); + } + + return 0; +} diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" new file mode 100644 index 0000000..81e9f13 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +map trees; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string str; + int total = 0; + + while (getline(cin, str)) + { + if (trees.count(str) == 0) + { + trees[str] = 1; + } + else + { + trees[str]++; + } + total++; + } + + vector > toVector(trees.begin(), trees.end()); + + sort(toVector.begin(), toVector.end()); + for (pair v : toVector) + { + printf("%s %.4f\n", v.first.c_str(), (float)v.second * 100 / total); + } + + return 0; +} diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" new file mode 100644 index 0000000..1ea1866 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int T, N; + +class TrieNode +{ +public: + TrieNode *next[10]; + bool isEnd = false; +}; + +class Trie +{ +public: + TrieNode *root = new TrieNode(); + + bool insert(string word) + { + TrieNode *current = root; + for (int i = 0; i < word.length(); i++) + { + int index = word[i] - '0'; + if (current->next[index] == NULL) + { + current->next[index] = new TrieNode(); + } + else if (i == word.length() - 1) + { + current = current->next[index]; + current->isEnd = true; + return false; + } + + current = current->next[index]; + if (current->isEnd) + return false; + } + current->isEnd = true; + return true; + } +}; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + int i, j; + string str; + + cin >> T; + + for (i = 0; i < T; i++) + { + + Trie TrieTree; + + bool flag = true; + cin >> N; + for (j = 0; j < N; j++) + { + cin >> str; + if (!TrieTree.insert(str)) + { + flag = false; + } + } + + if (flag) + { + cout << "YES\n"; + } + else + { + cout << "NO\n"; + } + } + + return 0; +} diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" new file mode 100644 index 0000000..b01a540 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" @@ -0,0 +1,97 @@ +#include + +using namespace std; + +int N; + +class Tree +{ + +public: + bool isEnd = false; + Tree *next[26]; + int nextcount = 0; + int count = 0; + + ~Tree() + { + for (int i = 0; i < 26; i++) + if (next[i] != NULL) + delete next[i]; + } +}; + +class TrieTree +{ + +public: + Tree *root = new Tree(); + + void insert(string str) + { + Tree *current = root; + + for (int i = 0; i < str.length(); i++) + { + int index = str[i] - 'a'; + if (current->next[index] == NULL) + { + current->next[index] = new Tree(); + current->nextcount++; + } + current = current->next[index]; + current->count++; + } + + current->isEnd = true; + } + + int dfs(Tree *current) + { + int res = 0; + for (int i = 0; i < 26; i++) + { + if (current->next[i] != NULL) + { + if (current->isEnd || current->nextcount != 1 || current == root) + { + res += current->next[i]->count; + } + res += dfs(current->next[i]); + } + } + return res; + } + + int ans() + { + int res = dfs(root); + return res; + } + + ~TrieTree() + { + delete root; + } +}; +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + while (cin >> N) + { + TrieTree trieTree; + for (int i = 0; i < N; i++) + { + string str; + cin >> str; + trieTree.insert(str); + } + + printf("%.2f\n", (double)trieTree.ans() / N); + } + + return 0; +} diff --git "a/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" "b/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" new file mode 100644 index 0000000..3e79871 --- /dev/null +++ "b/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int N, M, R; +int item[101]; + +#define INF 3000 +int d[101][101]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> R; + for (int i = 1; i <= N; i++) + { + cin >> item[i]; + for (int j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + int x, y, z; + + for (int i = 0; i < R; i++) + { + cin >> x >> y >> z; + d[x][y] = min(d[x][y], z); + d[y][x] = min(d[y][x], z); + } + + for (int k = 1; k <= N; k++) + { + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= N; j++) + { + d[i][j] = min(d[i][j], d[i][k] + d[k][j]); + } + } + } + + int ans = 0; + + for (int i = 1; i <= N; i++) + { + int temp = 0; + for (int j = 1; j <= N; j++) + { + if (d[i][j] <= M) + { + temp += item[j]; + } + } + ans = max(ans, temp); + } + cout << ans << "\n"; + + return 0; +} diff --git a/programmers/030601.cpp b/programmers/030601.cpp new file mode 100644 index 0000000..1e442c8 --- /dev/null +++ b/programmers/030601.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int gcd(int a, int b){ + if(a < b) swap(a, b); + + while(b!=0){ + int tmp = a%b; + a = b; + b = tmp; + } + return a; +} + +long long solution(int w, int h) { + long long answer = 1; + + answer = (long long)w*h - (w+h-gcd(w,h)); + + return answer; +} diff --git a/programmers/030801.cpp b/programmers/030801.cpp new file mode 100644 index 0000000..62481fc --- /dev/null +++ b/programmers/030801.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +#define INF 2100000000 + +int dist[201][201]; + +int solution(int n, int s, int a, int b, vector> fares) { + int answer = 0; + + for(int i=1; i<= n; i++){ + for(int j=1; j<=n; j++){ + dist[i][j] = INF; + } + dist[i][i] = 0; + } + + for(vector fare: fares){ + dist[fare[0]][fare[1]]=fare[2]; + dist[fare[1]][fare[0]]=fare[2]; + } + + for(int k=1; k<=n; k++){ + for(int i=1; i<=n; i++){ + for(int j=1; j<=n; j++){ + if((long long)dist[i][j] > (long long)dist[i][k] + dist[k][j]){ + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + + answer = dist[s][a] + dist[s][b]; + + for(int i=1; i<=n; i++){ + answer = min(answer, dist[s][i] + dist[i][a] + dist[i][b]); + } + + return answer; +} diff --git a/programmers/031401.cpp b/programmers/031401.cpp new file mode 100644 index 0000000..c73317e --- /dev/null +++ b/programmers/031401.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + +bool solution(vector> key, vector> lock) { + bool answer = false; + + int kx = key.size(); + int ky = key[0].size(); + int lx = lock.size(); + int ly = lock[0].size(); + + int tmp[20][20]; + int check[20][20]; + + for(int t=0; t<4 && !answer; t++){ + for(int x = -kx+1; x < lx && !answer; x++){ + for(int y = -ky+1; y < ly && !answer; y++){ + + bool flag = true; + + for(int i=0; i=0 && b>=0 && a < lx && b < ly){ + if(key[m][n]==0 && lock[a][b] == 0){ + flag = false; + } + else if(key[m][n]==1 && lock[a][b] == 1){ + flag = false; + } + else if(key[m][n]==1){ + check[a][b] = 1; + } + } + } + } + + if(flag){ + bool flag2 = true; + for(int i=0; i 0){ + gift[wants[i]]--; + answer++; + } + } + + return gift_cards.length - answer; + } +} + +class Solution { + private boolean[] include= new boolean[16]; + + public int solve(int n, int selected, int r, int[][] needs){ + if(n == needs[0].length && selected == r){ + int count = 0; + for(int i=0; i vec[] = new ArrayList[100001]; + + private void dfs(int n, int k, int[] passenger){ + visited[n] = true; + dist[n] = k; + for(int next : vec[n]){ + if(!visited[next]){ + dfs(next, k+passenger[next-1], passenger); + } + } + } + + public int[] solution(int n, int[] passenger, int[][] train) { + int[] answer = {0, 0}; + + for(int i=1; i<=n; i++){ + vec[i] = new ArrayList(); + visited[i] = false; + } + for(int i=0; i= maxcount){ + maxcount = dist[i]; + maxi = i; + } + } + answer[0] = maxi; + answer[1] = maxcount; + + return answer; + } + +} diff --git a/programmers/21040302.java b/programmers/21040302.java new file mode 100644 index 0000000..b6baff5 --- /dev/null +++ b/programmers/21040302.java @@ -0,0 +1,125 @@ +class Solution { + + public int[] solution(int[] lottos, int[] win_nums) { + int[] answer = {0,0}; + int c0 = 0, right = 0, wrong = 0; + for(int i=0; i<6; i++){ + boolean flag = false; + for(int j=0; j<6; j++){ + if(lottos[i] == win_nums[j]){ + flag = true; + break; + } + } + if(lottos[i] == 0) c0++; + else if(flag) right++; + else wrong++; + } + + answer[0] = right + c0; + answer[1] = right; + + for(int i=0; i<2; i++){ + if(answer[i] == 6) answer[i] = 1; + else if(answer[i] == 5) answer[i] = 2; + else if(answer[i] == 4) answer[i] = 3; + else if(answer[i] == 3) answer[i] = 4; + else if(answer[i] == 2) answer[i] = 5; + else answer[i] = 6; + } + return answer; + } +} + +class Solution { + public int[] solution(int rows, int columns, int[][] queries) { + int[] answer = new int[queries.length]; + int[][] board = new int[rows][]; + for(int i=0; i=y1-1; y--){ + temp[x2-1][y] = board[x2-1][y+1]; + } + for(int x=x2-2; x>=x1-1; x--){ + temp[x][y1-1] = board[x+1][y1-1]; + } + + int a = 100000000; + for(int x=0; x name; + + void check(int n, int money){ + if(n==-1) return; + if(money < 10){ + answer[n] += money; + } + else{ + int temp = (int)(money*0.1); + answer[n] += money - temp; + check(p[n], temp); + } + } + + public int[] solution(String[] enroll, String[] referral, String[] seller, int[] amount) { + answer = new int[enroll.length]; + p = new int[enroll.length]; + name = new HashMap<>(); + + name.put("-", -1); + for(int i=0; i +#include + +using namespace std; + +typedef struct node{ + struct node* previous; + struct node* next; + int n; + bool exist; +}node; + +node* A[1000000]; +int N; +int k; +node* now; +vector cmd; +vector stack; + +int main(){ + + cin >> N; + for(int i=0; in = i; + A[i]->exist = true; + } + + for(int i=0; inext = A[i+1]; + } + for(int i=1; iprevious = A[i-1]; + } + + A[0]->previous = A[N-1]; + A[N-1]->next = A[0]; + + cin >> k; + now = A[k]; + + for(string c:cmd){ + if(c[0] == 'U'){ + int x = atoi(c.substr(2, c.length()-2).c_str()); + while(x>0){ + now = now -> previous; + x--; + } + } + else if(c[0] == 'D'){ + int x = atoi(c.substr(2, c.length()-2).c_str()); + while(x>0){ + now = now->next; + x--; + } + } + else if(c[0] == 'C'){ + now->exist = false; + node* tmp = now->previous; + tmp->next = now->next; + node* tmp2 = now->next; + tmp2->previous = now->previous; + stack.push_back(now->n); + + if(now->n < now->next->n) + now = now->next; + else + now = now->previous; + } + else if(c[0] == 'Z'){ + int a = stack.back(); + stack.pop_back(); + + A[a]->exist = true; + A[a]->previous->next = A[a]; + A[a]->next->previous = A[a]; + } + } + + string ans = ""; + for(int i=0; iexist) ans += "O"; + else ans += "X"; + } + + return 0; +} diff --git a/programmers/210704.java b/programmers/210704.java new file mode 100644 index 0000000..8458878 --- /dev/null +++ b/programmers/210704.java @@ -0,0 +1,101 @@ +import java.util.Arrays; + +class Solution { + public int solution(int[] prices, int[] discounts) { + int answer = 0; + int idx = discounts.length-1; + + Arrays.sort(prices); + Arrays.sort(discounts); + + for(int p=prices.length-1; p>=0; p--){ + if(idx>=0){ + answer += prices[p]*(100-discounts[idx])/100; + idx--; + } + else { + answer += prices[p]; + } + } + return answer; + } +} + + +import java.util.*; + +class Solution { + public String[] solution(String s) { + //String[] answer = {}; + List answer = new LinkedList(); + + int leftlow = 0, lefthigh = 0; + int rightlow = s.length()-1, righthigh = s.length()-1; + int now = 0; + + while(leftlow<=rightlow){ + if(s.substring(leftlow, lefthigh+1).equals(s.substring(rightlow, righthigh+1))){ + + //System.out.println(s.substring(leftlow, lefthigh+1)); + if(leftlow != rightlow){ + answer.add(now, s.substring(leftlow, lefthigh+1)); + answer.add(answer.size()-now, s.substring(leftlow, lefthigh+1)); + now++; + } + else + answer.add(now, s.substring(leftlow, lefthigh+1)); + + leftlow = lefthigh+1; + lefthigh = leftlow; + righthigh = rightlow-1; + rightlow = righthigh; + } + else{ + lefthigh++; + rightlow--; + } + } + + return answer.toArray(new String[0]); + } +} + +import java.util.*; + +public class Solution { + public int solution(String s, String t) { + int result = 0; + + Stack stack = new Stack<>(); + + for(int i=0; i +#include + +using namespace std; + +int solution(int money, int minratio, int maxratio, int ranksize, int threshold, int months){ + for(int i=0; i maxratio) ratio = maxratio; + money = money - tmp / 100 * ratio; + } + return money; +} + +vector solution(int rows, int columns, vector > swipes){ + vector answer; + int board[51][51]; + int k = 1; + for(int i=1; i<=rows; i++){ + for(int j=1; j<=columns; j++){ + board[i][j] = k++; + } + } + + for(vector swipe: swipes){ + int ans = 0; + int dir = swipe[0]; + if(dir == 1){ + for(int j=swipe[2]; j<=swipe[4]; j++){ + int tmp = board[swipe[3]][j]; + for(int i=swipe[3]; i>swipe[1]; i--){ + board[i][j] = board[i-1][j]; + } + board[swipe[1]][j] = tmp; + ans += tmp; + } + } + else if(dir == 2){ + for(int j=swipe[2]; j<=swipe[4]; j++){ + int tmp = board[swipe[1]][j]; + for(int i=swipe[1]; iswipe[2]; j--){ + board[i][j] = board[i][j-1]; + } + board[i][swipe[2]] = tmp; + ans += tmp; + } + } + else{ + for(int i=swipe[1]; i<=swipe[3]; i++){ + int tmp = board[i][swipe[2]]; + for(int j=swipe[2]; j + +struct node{ + long long age; + int num, type; + bool operator<(const struct node& a) const{ + if(age == a.age){ + return num > a.num; + } + return age > a.age; + } +}; + +priority_queue pq; +vector> edges[100001]; +int degree[100001]; +bool off[100001]; + +vector solution(vector ages, vector> wires){ + vector answer; + + for(int i=1; i<=ages.size(); i++){ + pq.push({ages[i-1], i, 1}); + } + + for(vector wire : wires){ + edges[wire[0]].push_back({wire[1], wire[2]}); + degree[wire[1]]++; + } + + while(!pq.empty()){ + long long age = pq.top().age; + int now = pq.top().num; + int type = pq.top().type; + pq.pop(); + + if(off[now]) continue; + if(type==1){ + off[now]=true; + answer.push_back(now); + + for(pair edge: edges[now]){ + if(!off[edge.first]){ + pq.push({age+edge.second, edge.first, 2}); + } + } + } + else{ + degree[now]--; + if(degree[now]==0){ + off[now]=true; + answer.push_back(now); + + for(pair edge: edges[now]){ + if(!off[edge.first]){ + pq.push({age+edge.second, edge.first, 2}); + } + } + } + } + } + + return answer; +} \ No newline at end of file diff --git a/programmers/210718.cpp b/programmers/210718.cpp new file mode 100644 index 0000000..6769369 --- /dev/null +++ b/programmers/210718.cpp @@ -0,0 +1,89 @@ +vector edges[200001]; + +bool dfs(int n, int des){ + if(n == des) return true; + for(unsigned int i=0; i &A, vector &B){ + + for(unsigned int i = 0; i < A.size(); i++){ + edges[A[i]].push_back(B[i]); + edges[B[i]].push_back(A[i]); + } + + return dfs(1, N); +} + +string solution2(string riddle){ + string answer = ""; + for(int i=0; i0){ + check[answer[i-1]-'a'] = false; + } + if(i numbers){ + int count[31]; + for(int i=0; i<=30; i++) count[i] = 0; + for(unsigned i = 0; i < numbers.size(); i++){ + int now = numbers[i]; + int k = 0; + + while(now > 0){ + if(now%2 == 1){ + count[k]++; + } + now = now / 2; + k++; + } + } + int ans = 0; + for(int i=0; i<=30; i++){ + ans = max(ans, count[i]) + } + return ans; +} + +vector digits_sum[51]; + +int get_sum_of_digits(int n){ + if(n<10) return n; + return get_sum_of_digits(n / 10) + n%10; +} + +int solution5(vector numbers){ + for(unsigned int i=0; i 1){ + sort(digits_sum[i].begin(), digits_sum[i].end()); + unsigned int length = digits_sum[i].size(); + ans = max(ans, digits_sum[i][length-1] + digits_sum[i][length-2]); + } + } +} diff --git a/programmers/210912.py b/programmers/210912.py new file mode 100644 index 0000000..ae47416 --- /dev/null +++ b/programmers/210912.py @@ -0,0 +1,161 @@ +import math + +def solution(id_list, reports, k): + answer = [] + + cross_check = {} + count = {} + for i in range(len(id_list)): + cross_check[id_list[i]] = {} + cross_check[id_list[i]]['#'] = 0 + count[id_list[i]] = [] + + for report in reports: + reporter = report.split()[0] + reported = report.split()[1] + + if reported not in cross_check[reporter]: + cross_check[reporter][reported] = True + count[reported].append(reporter) + + answer = [] + + for id in id_list: + if len(count[id]) >= k: + for reporter in count[id]: + cross_check[reporter]['#'] += 1 + + for id in id_list: + answer.append(cross_check[id]['#']) + + return answer + +##################################### + +def dectok(n, k): + if n < k: + return str(n) + + return dectok(n//k, k) + str(n%k) + +def solve(n, left, right): + + if left >= right: + return 0 + + flag = False + idx = 0 + + for i in range(left, right): + if n[i] == "0": + flag = True + idx = i + break + + if flag: + return solve(n, left, idx) + solve(n, idx+1, right) + + flag = False + for i in range(2, int(math.sqrt(int(n[left:right])))+1): + if int(n[left:right]) % i == 0: + flag = True + break + + if int(n[left:right]) != 1 and flag == False: + print(int(n[left:right])) + return 1 + return 0 + +def solution2(n, k): + n = dectok(n, k) + + result = solve(n, 0, len(n)) + + return result + +def get_time(in_time, out_time): + in_time = int(in_time.split(":")[0])*60 + int(in_time.split(":")[1]) + + out_time = int(out_time.split(":")[0])*60 + int(out_time.split(":")[1]) + + return out_time - in_time + +############################### + +def get_fee(fees, park_time): + + if park_time <= fees[0]: + return fees[1] + + redunt = park_time - fees[0] + if redunt % fees[2] == 0: + return fees[1] + redunt // fees[2] * fees[3] + + return fees[1] + (redunt // fees[2] + 1) * fees[3] + +def solution3(fees, records): + + car_timer = {} + car_total_time = {} + car_fees = {} + + for record in records: + timer, car_num, inout = map(str, record.split()) + if inout == "IN": + car_timer[car_num] = timer + if car_num not in car_total_time: + car_total_time[car_num] = 0 + else: + car_total_time[car_num] += get_time(car_timer[car_num], timer) + car_timer[car_num] = "23:59" + + for car_num in car_timer.keys(): + car_total_time[car_num] += get_time(car_timer[car_num], "23:59") + car_fees[car_num] = get_fee(fees, car_total_time[car_num]) + + return [y[1] for y in sorted(car_fees.items(), key=lambda x : x[0])] + +print(solution3([1, 461, 1, 10],["00:00 1234 IN"])) + +############################### + +def solve4(n, info, arrow, score): + + arrow = list(arrow) + + if sum(arrow) > n: + return -1000000, [0,0,0,0,0,0,0,0,0,0,0] + + if score > 10: + return 0, tuple(arrow) + + origin = arrow[score] + arrow[score] += info[score]+1 + total1, res1 = solve4(n, info, tuple(arrow), score+1) + if total1 != -1000000: + total1 += 10-score + + arrow[score] = origin + total2, res2 = solve4(n, info, tuple(arrow), score+1) + if total2 != -1000000: + if info[score]>0: + total2 -= 10-score + + if total1 == -1000000 and total2 == -1000000: + return -1000000, [0,0,0,0,0,0,0,0,0,0,0] + if total1 > total2: + return total1, res1 + return total2, res2 + +def solution4(n, info): + answer = solve4(n, info, [0,0,0,0,0,0,0,0,0,0,0], 0) + if answer[0] < 0: + return [-1] + + answer = list(answer[1]) + if sum(answer) < n: + answer[10] += n-sum(answer) + return answer + +print(solution4(5, [2,1,1,1,0,0,0,0,0,0,0])) + diff --git "a/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" "b/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" new file mode 100644 index 0000000..8aa2a93 --- /dev/null +++ "b/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" @@ -0,0 +1,11 @@ +/* 10진수를 8진수로 변환 */ + +void dec_to_oct(int n){ + if(n<8){ + cout << n; + return; + } + + dec_to_oct(n/8); + cout << n % 8; +} \ No newline at end of file diff --git "a/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" "b/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" new file mode 100644 index 0000000..980b0a4 --- /dev/null +++ "b/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" @@ -0,0 +1,30 @@ +/* +문자열에서 반복되지 않는 첫번째 문자를 찾아내는 효율적인 함수를 작성하여라. • “total” => “o” • “teeter” => “r” +*/ + +#include +#include + +using namespace std; + +int count[26]; + +int main(){ + + string str; + + cin >> str; + + for(int i=0; i