Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mwy3055 committed May 18, 2023
2 parents f98d762 + 45487d7 commit 9b9bca3
Show file tree
Hide file tree
Showing 14 changed files with 926 additions and 0 deletions.
68 changes: 68 additions & 0 deletions baekjoon/cpp/c++/12996.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <bits/stdc++.h>

using ll = long long;
const ll MOD = 1e9 + 7;

int s, a, b, c, mval;
ll dp[51];
ll combi[51][51];

void getinput()
{
std::cin >> s >> a >> b >> c;
}

void init()
{
// combination
combi[0][0] = 1;
for (int i = 1; i <= 50; i++)
{
combi[i][0] = 1;
for (int j = 1; j <= i; j++)
{
combi[i][j] = (combi[i - 1][j - 1] + combi[i - 1][j]) % MOD;
}
}
}

ll comb(int i, int j)
{
return combi[i][j] % MOD;
}

ll solve(int song)
{
if (song <= 0)
return 0;
auto &ret = dp[song];
if (ret != -1)
return ret;

ret = comb(song, a);
ret = (ret * comb(song, b)) % MOD;
ret = (ret * comb(song, c)) % MOD;

for (int i = song - 1; i >= a; i--)
ret = (ret - (comb(song, i) * solve(i)) % MOD + MOD) % MOD;

return ret;
}

ll solve()
{
std::memset(dp, -1, sizeof(dp));
init();
if (a + b + c < s)
return 0;
return solve(s);
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}
60 changes: 60 additions & 0 deletions baekjoon/cpp/c++/14922.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>

using ll = long long;

int n;
std::vector<ll> a;

void getinput()
{
std::cin >> n;
for (int i = 0; i < n; i++)
{
int num;
std::cin >> num;
a.push_back(num);
}
}

int solve()
{
int ans1 = 0;
ll sum1 = (a[0] + a[1]) * 3;
for (int i = 1; i < n - 1; i++)
{
if ((a[i] + a[i + 1]) * 3 < sum1)
{
sum1 = (a[i] + a[i + 1]) * 3;
ans1 = i;
}
}
if (n == 2)
return ans1;

int ans2 = 0;
ll sum2 = (a[0] + a[1] + a[2]) * 2;
for (int i = 1; i < n - 2; i++)
{
if ((a[i] + a[i + 1] + a[i + 2]) * 2 < sum2)
{
sum2 = (a[i] + a[i + 1] + a[i + 2]) * 2;
ans2 = i;
}
}

if (sum1 < sum2)
return ans1;
else if (sum1 > sum2)
return ans2;
else
return std::min(ans1, ans2);
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}
44 changes: 44 additions & 0 deletions baekjoon/cpp/c++/15483.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>

std::string a, b;
int dp[1001][1001];

void getinput()
{
std::cin >> a >> b;
}

int solve()
{
int n = a.length(), m = b.length();

for (int i = 1; i <= n; i++)
{
dp[i][0] = i;
}
for (int j = 1; j <= m; j++)
{
dp[0][j] = j;
}

for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;
}
}
return dp[n][m];
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}
90 changes: 90 additions & 0 deletions baekjoon/cpp/c++/1572.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <bits/stdc++.h>

using ll = long long;
int n, k;
std::vector<int> numbers;

void getinput()
{
std::cin >> n >> k;
for (int i = 0; i < n; i++)
{
int num;
std::cin >> num;
numbers.push_back(num);
}
}

ll solve()
{
ll ans = 0;
int half = (k + 1) / 2;

if (k == 1)
{
for (auto &num : numbers)
ans += num;
return ans;
}

std::multiset<int, std::greater<int>> left;
std::multiset<int> right;

for (int i = 0; i < k - 1; i++)
{
left.insert(numbers[i]);
}

for (int i = k - 1; i < n; i++)
{
// add number[i]
right.insert(numbers[i]);
while (left.size() > half)
{
auto val = *(left.begin());
right.insert(val);
left.erase(left.begin());
}
while (right.size() > k - half)
{
auto val = *(right.begin());
left.insert(val);
right.erase(right.begin());
}
if (*(left.begin()) > *(right.begin()))
{
auto lval = *(left.begin());
auto rval = *(right.begin());
left.erase(left.begin());
right.erase(right.begin());
left.insert(rval);
right.insert(lval);
}
// calculate median
auto median = *(left.begin());
ans += median;
// erase number[i-k+1]
auto erase = numbers[i - k + 1];
auto it = left.find(erase);
if (it != left.end())
{
left.erase(it);
}
else
{
auto it2 = right.find(erase);
right.erase(it2);
}
}

return ans;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}
107 changes: 107 additions & 0 deletions baekjoon/cpp/c++/16118.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <bits/stdc++.h>

using ll = long long;
using edge = std::pair<ll, int>; // dist, dest
using node = std::pair<ll, int>; // current cost(dist), current position
using wnode = std::pair<ll, std::pair<int, bool>>; // cur cost, cur pos, cur state (run/walk)

const ll INF = 1LL << 60;
int n, m;
std::vector<edge> graph[4001];
ll fdist[4001], wdist[2][4001];

void getinput()
{
std::cin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, d;
std::cin >> a >> b >> d;
graph[a].emplace_back(d * 2, b);
graph[b].emplace_back(d * 2, a);
}
}

void run_fox()
{
std::priority_queue<node, std::vector<node>, std::greater<node>> pq;
std::memset(fdist, 0x3f, sizeof(fdist));

fdist[1] = 0;
pq.emplace(0, 1);
while (!pq.empty())
{
auto [cost, cur] = pq.top();
pq.pop();

if (cost > fdist[cur])
continue;

for (auto &[ncost, dest] : graph[cur])
{
if (cost + ncost < fdist[dest])
{
fdist[dest] = cost + ncost;
pq.emplace(cost + ncost, dest);
}
}
}
}

void run_wolf()
{
std::priority_queue<wnode, std::vector<wnode>, std::greater<wnode>> pq;
std::memset(wdist, 0x3f, sizeof(wdist));

// true: run, false: walk
// wdist[0][1] = wdist[1][1] = 0을 하면 안 되는 이유
// 1에서 출발하여 돌다가 다시 1로 돌아온 후, 느리게 걸어나가는 경로가 최적일 수도 있음
pq.emplace(0, std::make_pair(1, false));

while (!pq.empty())
{
auto top = pq.top();
ll cost = top.first, cur = top.second.first;
auto state = top.second.second;
pq.pop();

if (cost > wdist[state][cur])
continue;

auto nstate = !state;
for (auto &[ncost, dest] : graph[cur])
{
auto speed = nstate ? ncost / 2 : ncost * 2;
if (cost + speed < wdist[nstate][dest])
{
wdist[nstate][dest] = cost + speed;
pq.emplace(cost + speed, std::make_pair(dest, nstate));
}
}
}
}

int solve()
{
run_fox();
run_wolf();

int ans = 0;
for (int i = 2; i <= n; i++)
{
if (fdist[i] < std::min(wdist[0][i], wdist[1][i]))
{
ans++;
}
}
return ans;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}
Loading

0 comments on commit 9b9bca3

Please sign in to comment.