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

45-9kyo-hwang #166

Merged
merged 5 commits into from
May 27, 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
79 changes: 79 additions & 0 deletions 9-kyo-hwang/Backtracking/6987 μ›”λ“œμ»΅.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> TeamMatches
{
{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5},
{1, 2}, {1, 3}, {1, 4}, {1, 5},
{2, 3}, {2, 4}, {2, 5},
{3, 4}, {3, 5},
{4, 5},
};

const vector<pair<int, int>> MatchResultIndices
{// 1 win, tie, 2 win
{0, 2}, {1, 1}, {2, 0}
};

vector<vector<int>> MatchScore;

bool Backtracking(int Match = 0)
{
if(Match == TeamMatches.size())
{
for(const auto& Team : MatchScore)
{
if(count(Team.begin(), Team.end(), 0) != 3)
{
return false;
}
}

return true;
}

const auto& [Team1, Team2] = TeamMatches[Match];
for(const auto& [i, j] : MatchResultIndices)
{
if(MatchScore[Team1][i] <= 0 || MatchScore[Team2][j] <= 0)
{
continue;
}

--MatchScore[Team1][i];
--MatchScore[Team2][j];

if(Backtracking(Match + 1))
{
return true;
}

++MatchScore[Team1][i];
++MatchScore[Team2][j];
}

return false;
}

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

for(int T = 1; T <= 4; ++T)
{
MatchScore.assign(6, vector(3, 0));
for(auto& Team : MatchScore)
{
for(auto& Score : Team)
{
cin >> Score;
}
}
cout << Backtracking() << " ";
}

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

using namespace std;

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

int N; cin >> N;
vector<pair<int, int64_t>> Stack;

int64_t NumPair = 0;
while(N--)
{
int Height; cin >> Height;
int64_t NumSame = 1;

while(!Stack.empty() && Stack.back().first <= Height)
{
NumPair += Stack.back().second;
if(Stack.back().first == Height)
{
NumSame = Stack.back().second + 1;
}
Stack.pop_back();
}

if(!Stack.empty())
{
NumPair += 1;
}
Stack.emplace_back(Height, NumSame);
}

cout << NumPair;

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;
}
5 changes: 4 additions & 1 deletion 9-kyo-hwang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@
| 39μ°¨μ‹œ | 2024.3.27 | Divide and Conquer | [18291 λΉ„μš”λœ¨μ˜ 징검닀리 κ±΄λ„ˆκΈ°](https://www.acmicpc.net/problem/18291) | [#150](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/150) |
| 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) |
| 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/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) |
Loading