Skip to content

Commit

Permalink
Merge pull request #124 from donghoony/main
Browse files Browse the repository at this point in the history
갈!
  • Loading branch information
jminkkk authored Aug 19, 2024
2 parents afd6432 + 20b5013 commit 10ca93d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 2 deletions.
37 changes: 37 additions & 0 deletions week16/A_3061/donghoony/3061.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

int a[1000], s[1000], N, ans;

void f(int l, int r){
if (l > r) return;
if (l == r){
s[l] = a[l];
return;
}
int m = (l + r) / 2;
f(l, m); f(m+1, r);
int i = l, j = m+1, k = l;
while(i <= m && j <= r){
if (a[i] < a[j]) s[k++] = a[i++];
else s[k++] = a[j++], ans += m - i + 1;
}
while(i <= m) s[k++] = a[i++];
while(j <= r) s[k++] = a[j++];
for(int i = l; i <= r; i++) a[i] = s[i];
}

void solve() {
cin >> N;
ans = 0;
for(int i = 0; i < N; i++) cin >> a[i];
f(0, N-1);
cout << ans << '\n';
}

int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int T; cin >> T;
while (T--) solve();
return 0;
}
6 changes: 6 additions & 0 deletions week16/A_3061/donghoony/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Info
[Link](https://boj.kr/3061)
## 💡 풀이 방법 요약
사다리를 하나 그리기 = 두 원소를 Swap
가장 빠르게 원하는 배열로 만들기: Counting inversion과 같은 문제 (이건 경험이 필요하긴 하다...)
N^2로도 해결 가능하지만, Merge sort를 활용한 O(nlogn) 풀이가 빠르게 통과

## 👀 실패 이유

## 🙂 마무리
https://github.com/KU-AlKon/study/blob/master/2023-2-advanced/01-Divide%20%26%20Conquer.pdf

중간의 Counting inversion을 참고해도 좋을 듯 !
33 changes: 33 additions & 0 deletions week16/B_25634/donghoony/25634.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 0x3f3f3f3f;

ll N, L, a[202020], on[202020], off[202020], d[202020], ans, t;

ll f(int x) {
if (x > N) return 0;
ll& ret = d[x];
if (ret != -1) return ret;
ret = off[x] - off[x-1];
ret += max(f(x+1), on[N] - on[x]);
return ret;
}

int main() {
cin.tie(nullptr)->sync_with_stdio(false);
memset(d, -1, sizeof d);
cin >> N;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = 1; i <= N; i++) {
cin >> t;
off[i] += off[i-1], on[i] += on[i-1];
if (t == 0) off[i] += a[i];
else on[i] += a[i];
}

for (int i = 1; i <= N; i++) ans = max(ans, on[i-1] + f(i));
cout << ans << '\n';
return 0;
}
4 changes: 3 additions & 1 deletion week16/B_25634/donghoony/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Info
[Link](https://boj.kr/25634)
## 💡 풀이 방법 요약
dp[i]: i번째부터 **켰을 때** 얻을 수 있는 최대 점수 -> 구해야하는 정답은 max(dp)이다.
dp[i]는 이후를 토글링하지 않고 얻을 수 있는 점수를 더하거나, 다음 것을 켠 것 중 큰 값이다

## 👀 실패 이유

누적합 배열에서 한 값을 꺼내는 것을 까먹었다 -> `off[x] - off[x-1]`
## 🙂 마무리
17 changes: 17 additions & 0 deletions week16/C_30108/donghoony/30108-faster.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 0x3f3f3f3f;

ll N, t, a[303030], ans;
string s;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> N; cin.ignore();
getline(cin, s);
for (int i = 0; i < N; i++) cin >> a[i];
sort(a, a+N, greater<>());
for (int i = 0; i < N; i++) ans += a[i], cout << ans << '\n';
return 0;
}
27 changes: 27 additions & 0 deletions week16/C_30108/donghoony/30108-pq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 0x3f3f3f3f;

ll N, t, a[303030], ans;
vector<int> g[303030];
priority_queue<pair<int, int>> pq;

int main() {
cin.tie(nullptr)->sync_with_stdio(false);\
cin >> N;
for (int i = 2; i <= N; i++) {
cin >> t;
g[t].push_back(i);
}
for (int i = 1; i <= N; i++) cin >> a[i];
pq.emplace(a[1], 1);
while (!pq.empty()) {
auto [val, cur] = pq.top(); pq.pop();
for (auto& nxt : g[cur]) pq.emplace(a[nxt], nxt);
ans += val;
cout << ans << '\n';
}
return 0;
}
3 changes: 3 additions & 0 deletions week16/C_30108/donghoony/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Info
[Link](https://boj.kr/30108)
## 💡 풀이 방법 요약
간단한 풀이로는: 꺼낼 수 있는 정점을 자료구조에 담고 가장 큰 값을 꺼내어나가기, 그 자식 노드를 추가하기를 반복하기.
문제를 조금 관찰하면 부모가 항상 크다는 점을 알 수 있고, 전체 정점을 내림차순 정렬한 뒤 하나씩 더해나가도 답을 구할 수 있다.
부모가 항상 더 큰 값이기 때문에, 부모 정점이 제일 크고, 이를 제외한 가장 큰 정점 두 개는 재귀적으로 정당성을 증명할 수 있다!

## 👀 실패 이유

Expand Down
27 changes: 27 additions & 0 deletions week16/D_11918/donghoony/11918.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3f;

ll N, L, ans;
pair<ll, ll> arr[151515];

int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> N >> L;
for (int i = 0; i < N; i++) {
ll t; cin >> t;
arr[i] = {t - L, t + L};
}
sort(arr, arr+N);
ll bf = -LINF;
for (int i = 0; i < N-1; i++) {
ans += max(0LL, arr[i].second - max(arr[i+1].first, bf));
bf = arr[i].second;
}
cout << ans << '\n';

return 0;
}
4 changes: 3 additions & 1 deletion week16/D_11918/donghoony/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Info
[Link](https://boj.kr/11918)
## 💡 풀이 방법 요약

문제를 관찰하면... 결국 겹치는 부분을 최대화하라는 것이 된다. 즉 겹치는 부분의 길이를 구하라는 것과 동치다.
각각의 점에 대해서 -L, +L한 값을 쌍으로 넣어 두고, 정렬한 뒤 스위핑하면서 값을 갱신한다.
수직선상에 두 개 이상 겹치는 구간의 길이를 구해야 하는데, 세 개 이상 겹치는 경우가 발생하지 않도록 잘 조정해야 한다.
## 👀 실패 이유

## 🙂 마무리

0 comments on commit 10ca93d

Please sign in to comment.