Skip to content

Commit

Permalink
22.09.16
Browse files Browse the repository at this point in the history
  • Loading branch information
delena0702 authored Sep 16, 2022
1 parent f6995c1 commit 41e4ddc
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BAEKJOON/11047_동전_0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>

#define SIZE 10

using namespace std;

int arr[SIZE];

int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);

int N, K;
cin >> N >> K;

for (int i = 0; i < N; i++)
cin >> arr[i];

int answer = 0;
for (int i = N - 1; i >= 0; i--)
{
answer += K / arr[i];
K -= (K / arr[i]) * arr[i];
}
cout << answer << endl;

return 0;
}
31 changes: 31 additions & 0 deletions BAEKJOON/11722_가장_긴_감소하는_부분_수열.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>

#define SIZE 1000

using namespace std;

int arr[SIZE], dp[SIZE];

int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);

int N;
cin >> N;
for (int i = 0; i < N; i++)
cin >> arr[i];

int answer = 1;
dp[0] = 1;
for (int i = 1; i < N; i++)
{
dp[i] = 1;
for (int j = 0; j < i; j++)
if (arr[j] > arr[i])
dp[i] = max(dp[i], dp[j] + 1);
answer = max(answer, dp[i]);
}
cout << answer << endl;

return 0;
}
29 changes: 29 additions & 0 deletions BAEKJOON/11729_하노이_탑_이동_순서.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>

using namespace std;

int N;

void solve(int cnt, int a, int b, int c)
{
if (cnt == 1)
{
cout << a << ' ' << b << '\n';
return;
}
solve(cnt - 1, a, c, b);
cout << a << ' ' << b << '\n';
solve(cnt - 1, c, b, a);
}

int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;

cout << (1 << N) - 1 << '\n';
solve(N, 1, 3, 2);

return 0;
}
155 changes: 155 additions & 0 deletions BAEKJOON/13510_트리와_쿼리_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <bits/stdc++.h>

#define SIZE 100010

using namespace std;

struct Edge {
int a, b, w;
};

vector<int> edges[SIZE + 1];
int edge_raw[SIZE + 1][3];
int edge_idx[SIZE + 1];
int N;

const int seg = 1 << 18;
int segtree[1 << 19];

void update(int idx, int value)
{
idx |= seg;
segtree[idx] = value;
while (idx >>= 1)
segtree[idx] = max(segtree[idx << 1], segtree[idx << 1 | 1]);
}

int query(int l, int r)
{
int retval = 0;
for (l |= seg, r |= seg; l <= r; l >>= 1, r >>= 1)
{
if (l & 1)
retval = max(retval, segtree[l++]);
if (~r & 1)
retval = max(retval, segtree[r--]);
}
return retval;
}

vector<int> tree[SIZE + 1];
vector<bool> check;
int depth[SIZE + 1], parent[SIZE + 1], sz[SIZE + 1];
int order[SIZE + 1], top[SIZE + 1];

void dfs(int here)
{
check[here] = true;
for (auto & there : edges[here]) {
if (check[there])
continue;
tree[here].push_back(there);
dfs(there);
}
}

void dfs1(int here)
{
sz[here] = 1;
for (auto & there : tree[here]) {
depth[there] = depth[here] + 1;
parent[there] = here;
dfs1(there);

sz[here] += sz[there];
if (sz[there] > sz[tree[here][0]])
swap(there, tree[here][0]);
}
}

int counter = 0;
void dfs2(int here)
{
order[here] = ++counter;
for (auto &there : tree[here])
{
top[there] = ((there == tree[here][0]) ? top[here] : there);
dfs2(there);
}
}

void hld_update(int idx, int value)
{
update(order[idx], value);
}

int hld_query(int a, int b)
{
int retval = 0;
while (top[a] != top[b])
{
if (depth[top[a]] < depth[top[b]])
swap(a, b);

retval = max(retval, query(order[top[a]], order[a]));
a = parent[top[a]];
}
if (depth[a] > depth[b])
swap(a, b);
retval = max(retval, query(order[a] + 1, order[b]));
return retval;
}

int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> N;
for (int i = 0; i < N - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
edges[u].push_back(v);
edges[v].push_back(u);

edge_raw[i][0] = u;
edge_raw[i][1] = v;
edge_raw[i][2] = w;
}

check.resize(N + 1);
dfs(1);
depth[1] = 1;
dfs1(1);
top[1] = 1;
dfs2(1);

for (int i = 0; i < N - 1; i++)
{
auto &[a, b, w] = edge_raw[i];
edge_idx[i + 1] = (depth[a] > depth[b]) ? a : b;
hld_update(edge_idx[i + 1], w);
}

int M;
cin >> M;
for (int i = 0; i < M; i++)
{
int o, a, b;
cin >> o >> a >> b;

switch (o)
{
case 1:
hld_update(edge_idx[a], b);
break;

case 2:
cout << hld_query(a, b) << '\n';
break;
}
}

return 0;
}
65 changes: 65 additions & 0 deletions BAEKJOON/14888_연산자_끼워넣기.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>

#define SIZE 11
#define INF 1000000001

using namespace std;

int N;
int arr[SIZE];
int ops[4];
int answer[2] = {INF, -INF};

void solve(int cnt, int pvalue)
{
if (cnt == N)
{
answer[0] = min(answer[0], pvalue);
answer[1] = max(answer[1], pvalue);
return;
}

for (int i = 0; i < 4; i++)
{
if (ops[i] == 0)
continue;

int nvalue = pvalue;
switch (i)
{
case 0:
nvalue += arr[cnt];
break;
case 1:
nvalue -= arr[cnt];
break;
case 2:
nvalue *= arr[cnt];
break;
case 3:
nvalue /= arr[cnt];
break;
}

ops[i]--;
solve(cnt + 1, nvalue);
ops[i]++;
}
}

int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> N;
for (int i = 0; i < N; i++)
cin >> arr[i];
for (int i = 0; i < 4; i++)
cin >> ops[i];
solve(1, arr[0]);
cout << answer[1] << endl
<< answer[0] << endl;

return 0;
}
50 changes: 50 additions & 0 deletions BAEKJOON/1520_내리막_길.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int N, M;
int arr[505][505];
ll dp[505][505];
int darr[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll solve(int x, int y)
{
if (x == M - 1 && y == N - 1)
return 1;
if (dp[y][x] != -1)
return dp[y][x];

ll &retval = dp[y][x];

retval = 0;
for (auto &[dx, dy] : darr)
{
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= M || ny < 0 || ny >= N)
continue;
if (arr[y][x] <= arr[ny][nx])
continue;
retval += solve(nx, ny);
}
return retval;
}

int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);

cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
dp[i][j] = -1;
}
}

cout << solve(0, 0) << '\n';

return 0;
}
Loading

0 comments on commit 41e4ddc

Please sign in to comment.