Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

50-9kyo-hwang #181

Merged
merged 6 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions 9-kyo-hwang/Brute Force/14500 ν…ŒνŠΈλ‘œλ―Έλ…Έ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int N, M;
vector<vector<int>> Paper;
vector<vector<bool>> Visited;

bool OutOfBound(int x, int y)
{
return x < 0 || x >= N || y < 0 || y >= M;
}

int Backtracking(int x, int y, int FigureCount = 1, int Sum = 0)
{
if(OutOfBound(x, y) || Visited[x][y])
{
return 0;
}
else if(FigureCount > 4)
{
return Sum;
}
else
{
Visited[x][y] = true;
Sum += Paper[x][y];
int Answer = 0;

for(const auto& [dx, dy] : Offset)
{
Answer = max(Answer, Backtracking(x + dx, y + dy, FigureCount + 1, Sum));
}

Visited[x][y] = false;
Sum -= Paper[x][y];
return Answer;
}
}

int CalculateShapeT(int x, int y)
{
int Sum = Paper[x][y];
int MinCell = 1001;
int SelectCnt = 1;
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny))
{
continue;
}

MinCell = min(MinCell, Paper[nx][ny]);
Sum += Paper[nx][ny];
SelectCnt++;
}

if(SelectCnt < 4) // is not Shape T
{
return 0;
}

if(SelectCnt == 5) // Shape + -> Shape T
{
Sum -= MinCell;
}

return Sum;
}

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

cin >> N >> M;
Paper.assign(N, vector(M, 0));
Visited.assign(N, vector(M, false));

for(auto& Row : Paper)
{
for(auto& Col : Row)
{
cin >> Col;
}
}

int Answer = 0;
for(int x = 0; x < N; ++x)
{
for(int y = 0; y < M; ++y)
{
Answer = max(Answer, Backtracking(x, y)); // except Shape T
Answer = max(Answer, CalculateShapeT(x, y)); // Shape T
}
}

cout << Answer;

return 0;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <set>
#include <unordered_map>

using namespace std;

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

int N; cin >> N;
multiset<pair<int, int>> DB;
unordered_map<int, int> LevelMap;

for(int i = 0; i < N; ++i)
{
int P, L; cin >> P >> L;
DB.emplace(L, P);
LevelMap.emplace(P, L);
}

int M; cin >> M;
while(M--)
{
string Command; cin >> Command;
if(Command == "recommend")
{
int x; cin >> x;
if(x == 1)
{
const auto& [L, P] = *(--DB.end());
cout << P << "\n";
}
else if(x == -1)
{
const auto& [L, P] = *DB.begin();
cout << P << "\n";
}
}
else if(Command == "add")
{
int P, L; cin >> P >> L;
DB.emplace(L, P);
LevelMap.emplace(P, L);
}
else if(Command == "solved")
{
int P; cin >> P;
int L = LevelMap[P];

DB.erase(DB.find({L, P}));
LevelMap.erase(P);
}
}

return 0;
}
55 changes: 55 additions & 0 deletions 9-kyo-hwang/Dynamic Programming/2240 μžλ‘λ‚˜λ¬΄.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

int T, W; cin >> T >> W;
vector<int> Poses(T + 1, 0);
for(int t = 1; t <= T; ++t)
{
cin >> Poses[t];
Poses[t] -= 1;
}

vector<vector<vector<int>>> DP(T + 1, vector(W + 1, vector(2, 0)));

for(int Walk = 0; Walk <= W; ++Walk)
{
for(int Time = 1; Time <= T; ++Time)
{
if(Time == 1)
{
DP[Time][Walk][0] = DP[Time - 1][Walk][0] + (Poses[Time] == 0);
if(Walk > 0)
{
DP[Time][Walk][1] = DP[Time - 1][Walk - 1][0] + (Poses[Time] == 1);
}
continue;
}

if(Walk == 0)
{
DP[Time][Walk][0] = DP[Time - 1][Walk][0] + (Poses[Time] == 0);
DP[Time][Walk][1] = DP[Time - 1][Walk][1] + (Poses[Time] == 1);
continue;
}

DP[Time][Walk][0] = max(DP[Time - 1][Walk][0], DP[Time - 1][Walk - 1][1]) + (Poses[Time] == 0);
DP[Time][Walk][1] = max(DP[Time - 1][Walk - 1][0], DP[Time - 1][Walk][1]) + (Poses[Time] == 1);
}
}

int Ans = 0;
for(int Walk = 0; Walk <= W; ++Walk)
{
Ans = max({Ans, DP[T][Walk][0], DP[T][Walk][1]});
}
cout << Ans;

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <vector>

using namespace std;

void DFSHelper(const vector<vector<int>>& Graph, vector<bool>& Visited, int SrcNode = 1)
{
Visited[SrcNode] = true;
for(const auto& DstNode : Graph[SrcNode])
{
if(!Visited[DstNode])
{
DFSHelper(Graph, Visited, DstNode);
}
}
}

bool DFS(const int N, vector<vector<int>>& Graph)
{
vector<bool> Visited(N + 1, false);
DFSHelper(Graph, Visited);

for(int Node = 1; Node <= N; ++Node)
{
if(!Visited[Node])
{
return false;
}
}
return true;
}

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

int N, M; cin >> N >> M;

vector<vector<int>> G(N + 1);
vector<vector<int>> GC(N + 1);

while(M--)
{
int v, w; cin >> v >> w;
G[v].emplace_back(w);
GC[w].emplace_back(v);
}

cout << (DFS(N, G) && DFS(N, GC) ? "Yes" : "No");

return 0;
}
10 changes: 8 additions & 2 deletions 9-kyo-hwang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
| 40μ°¨μ‹œ | 2024.4.2 | Dynamic Programming | [7579 μ•±](https://www.acmicpc.net/problem/7579) | [#153](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/153) |
| 41μ°¨μ‹œ | 2024.4.7 | Prefix Sum | [2015 μˆ˜λ“€μ˜ ν•© 4](https://www.acmicpc.net/problem/2015) | [#155](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/155) |
| 42μ°¨μ‹œ | 2024.4.29 | Minimum Spanning Tree(MST) | [17472 닀리 λ§Œλ“€κΈ° 2](https://www.acmicpc.net/problem/17472) | [#159](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/159) |
| 43μ°¨μ‹œ | 2024.4.28 | Data Structure | [3015 μ˜€μ•„μ‹œμŠ€ μž¬κ²°ν•©](https://www.acmicpc.net/problem/3015) | [#162](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/161) |
| 44μ°¨μ‹œ | 2024.5.6 | Backtracking | [6987 μ›”λ“œμ»΅](https://www.acmicpc.net/problem/6987) | [#164](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/161) |
| 43μ°¨μ‹œ | 2024.4.28 | Data Structure | [3015 μ˜€μ•„μ‹œμŠ€ μž¬κ²°ν•©](https://www.acmicpc.net/problem/3015) | [#162](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/162) |
| 44μ°¨μ‹œ | 2024.5.6 | Backtracking | [6987 μ›”λ“œμ»΅](https://www.acmicpc.net/problem/6987) | [#164](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/164) |
| 45μ°¨μ‹œ | 2024.5.9 | Dynamic Programming | [2240 μžλ‘λ‚˜λ¬΄](https://www.acmicpc.net/problem/2240) | [#166](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/166) |
| 46μ°¨μ‹œ | 2024.5.13 | Simulation | [14503 λ‘œλ΄‡ μ²­μ†ŒκΈ°](https://www.acmicpc.net/problem/14503) | [#167](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/167) |
| 47μ°¨μ‹œ | 2024.5.16 | Brute Force | [14500 ν…ŒνŠΈλ‘œλ―Έλ…Έ](https://www.acmicpc.net/problem/14503) | [#171](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/171) |
| 48μ°¨μ‹œ | 2024.5.20 | Shortest Path | [1162 λ„λ‘œν¬μž₯](https://www.acmicpc.net/problem/1162) | [#175](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/175) |
| 49μ°¨μ‹œ | 2024.5.23 | Data Structure | [21939 문제 μΆ”μ²œ μ‹œμŠ€ν…œ Version 1](https://www.acmicpc.net/problem/21939) | [#179](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/179) |
| 50μ°¨μ‹œ | 2024.5.25 | Graph Traversal | [26146 즉ν₯ μ—¬ν–‰ (Easy)](https://www.acmicpc.net/problem/26146) | [#182](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/182) |
Loading
Loading