-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from donghoony/main
갈!
- Loading branch information
Showing
9 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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을 참고해도 좋을 듯 ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]` | ||
## 🙂 마무리 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# Info | ||
[Link](https://boj.kr/11918) | ||
## 💡 풀이 방법 요약 | ||
|
||
문제를 관찰하면... 결국 겹치는 부분을 최대화하라는 것이 된다. 즉 겹치는 부분의 길이를 구하라는 것과 동치다. | ||
각각의 점에 대해서 -L, +L한 값을 쌍으로 넣어 두고, 정렬한 뒤 스위핑하면서 값을 갱신한다. | ||
수직선상에 두 개 이상 겹치는 구간의 길이를 구해야 하는데, 세 개 이상 겹치는 경우가 발생하지 않도록 잘 조정해야 한다. | ||
## 👀 실패 이유 | ||
|
||
## 🙂 마무리 |