diff --git a/InSange/BFS/18352.cpp b/InSange/BFS/18352.cpp new file mode 100644 index 0000000..defc4c1 --- /dev/null +++ b/InSange/BFS/18352.cpp @@ -0,0 +1,143 @@ +//#include +//#include +//#include +// +//using namespace std; +// +//int N, M, K, X, a, b, minDist; +//deque> town; +//queue q; +//vector dist; +//vector answer; +// +//void Solve() +//{ +// cin >> N >> M >> K >> X; +// N++; +// town.assign(N, deque()); +// dist.assign(N, -1); +// +// for (int i = 0; i < M; i++) +// { +// cin >> a >> b; +// +// town[a].push_back(b); +// } +// +// minDist = -1; +// q.push(X); +// dist[X] = 0; +// +// while (!q.empty()) +// { +// int cur = q.front(); +// q.pop(); +// +// for (const int& val : town[cur]) +// { +// if (dist[val] != -1) continue; +// q.push(val); +// dist[val] = dist[cur] + 1; +// minDist = max(minDist, dist[val]); +// } +// } +// +// for (int i = 0; i < dist.size(); i++) +// { +// if (dist[i] == K) answer.push_back(i); +// } +// +// if (answer.size()) +// { +// for (const int& val : answer) +// { +// cout << val << "\n"; +// } +// } +// else cout << -1; +//} +// +//int main() +//{ +// cin.tie(nullptr); +// ios::sync_with_stdio(false); +// +// Solve(); +// +// return 0; +//} + +#include +#include +#include + +using namespace std; + +int N, M, K, X, A, B; +int cur, dist; +queue> q; +vector> towns; +vector visited; +priority_queue, greater> answer; + +void Solve() +{ + cin >> N >> M >> K >> X; + + N++; + towns.assign(N, vector()); + visited.assign(N, false); + + while (M--) + { + cin >> A >> B; + towns[A].push_back(B); + } + + q.push({ X, 0 }); + visited[X] = true; + + while (!q.empty()) + { + cur = q.front().first; + dist = q.front().second; + + q.pop(); + + if (dist == K) + { + answer.push(cur); + continue; + } + + for (auto v : towns[cur]) + { + if (visited[v]) continue; + q.push({ v, dist + 1 }); + visited[v] = true; + } + } + + if (answer.empty()) + { + cout << -1; + return; + } + + while (!answer.empty()) + { + cout << answer.top() << "\n"; + answer.pop(); + } + return; +} + +int main() +{ + cin.tie(nullptr); + ios::sync_with_stdio(false); + + Solve(); + + return 0; +} \ No newline at end of file diff --git a/InSange/DP/2565.cpp b/InSange/DP/2565.cpp new file mode 100644 index 0000000..b2abd96 --- /dev/null +++ b/InSange/DP/2565.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +int N, a, b, answer; +vector> line; +vector dp; + +void Solve() +{ + cin >> N; + + answer = 0; + dp.assign(N + 1, 1); + + for (int i = 0; i < N; i++) + { + cin >> a >> b; + line.push_back({ a, b }); + } + + sort(line.begin(), line.end()); + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < i; j++) + { + //cout << line[i].first << ", " << line[j].first << " \n"; + if (line[j].second < line[i].second) { + dp[i] = max(dp[i], dp[j] + 1); + //cout << line[i].first << "-> i dp : " << dp[i] << "\n"; + //cout << line[j].first << "-> j dp : " << dp[j] << "\n"; + } + } + + answer = max(answer, dp[i]); + } + + cout << N - answer; +} + +int main() +{ + cin.tie(nullptr); + ios::sync_with_stdio(false); + + Solve(); + + return 0; +} \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index a29d500..42335a6 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -11,6 +11,10 @@ | 7차시 | 2024.04.04 | 스택 | [문자열 폭발](https://www.acmicpc.net/problem/9935) | [#7](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] | 8차시 | 2024.04.09 | 투 포인터 | [두 용액](https://www.acmicpc.net/problem/2470) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/32)] | 9차시 | 2024.04.12 | 힙 | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] -| 10차시 | 2024.05.02 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)] -| 11차시 | 2024.05.02 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41)] +| 10차시 | 2024.04.30 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)] +| 11차시 | 2024.05.03 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41] +| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/46)] +| 13차시 | 2024.05.22 | BFS | [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/53)] +| 14차시 | 2024.05.27 | 투 포인터 | [포도주 시음](https://www.acmicpc.net/problem/31589) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] +======= --- diff --git "a/InSange/\355\210\254\355\217\254\354\235\270\355\204\260/31589.cpp" "b/InSange/\355\210\254\355\217\254\354\235\270\355\204\260/31589.cpp" new file mode 100644 index 0000000..d0ecc02 --- /dev/null +++ "b/InSange/\355\210\254\355\217\254\354\235\270\355\204\260/31589.cpp" @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; + +int N, K, l, r, lastDrink; +vector wine; +bool isLeft = false; +long long answer = 0; + +void Solve() +{ + cin >> N >> K; + + wine.assign(N, 0); + + for (int i = 0; i < N; i++) + { + cin >> wine[i]; + } + + sort(wine.begin(), wine.end()); + + l = 0; + r = N - 1; + answer = 0; + lastDrink = 0; + + while (K--) + { + if (isLeft == true) // + { + lastDrink = wine[l]; + l++; + } + else// if(isLeft == false) // ִ + { + answer += wine[r] - lastDrink; + r--; + } + + isLeft = (isLeft + 1) & 1; + } + + cout << answer; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + Solve(); + + return 0; +} \ No newline at end of file