Skip to content

Commit

Permalink
2024-03-24 21:44:21
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/at-coder-beginner-contest-346-up-solve.md
  • Loading branch information
gyunseo committed Mar 24, 2024
1 parent 387e473 commit 48f9fee
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
10 changes: 0 additions & 10 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
"source": false
}
}
},
{
"id": "f7153e1a74759abd",
"type": "leaf",
"state": {
"type": "excalidraw",
"state": {
"file": "Excalidraw/Drawing 2024-03-24 18.59.55.excalidraw.md"
}
}
}
]
}
Expand Down
50 changes: 50 additions & 0 deletions src/content/blog/at-coder-beginner-contest-346-up-solve.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,53 @@ int main() {
### 목표: `00011`과 같은 수열에서 toggle cost를 최소로 하며, sub sequences에서 adjacent characters가 동일한 수열의 원소가 딱 두개만 되게 만드는 것
$N = 2 \times 10^5$ 이라서 $O(NlogN)$ 이거나 $O(N)$ 근데 정렬을 시키거나 이분 탐색을 해서, 뭔가를 얻을 건덕지는 없어 보이고, 그러면 $O(N)$인데 이거는 그리디 말고 DP로 풀어야 할 거 같다는 느낌적인 느낌이 들었습니다.
```cpp
#include <bits/stdc++.h>
#define endl '\n'
#define VI vector<int>
#define watch(x) cout << (#x) << " is " << x << endl
#define FOR(s, e) for (int i = s; i < e; i++)
using namespace std;
using ll = long long;
#define VL vector<long long>
const int MAX = 200000;
const ll INF = (int)1e18;
int N;
string S;
// dp table은 i번째 문자가 k로 되고, j만큼의 adjacent character pair가 있을 때
// min cost table이다
ll dp[MAX + 1][2][2];
VL costs;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
cin.ignore();
getline(cin, S);
for (int i = 0; i < N; i++) {
int cost;
cin >> cost;
costs.push_back(cost);
}
fill(&dp[0][0][0], &dp[MAX - 1][1][2], INF);
dp[0][0][S[0] - '0'] = 0;
dp[0][0][(S[0] - '0') ^ 1] = costs[0];
FOR(1, N) {
int cur_digit = S[i] - '0';
for (int k = 0; k < 2; k++) {
dp[i][0][k] = dp[i - 1][0][k ^ 1] + (cur_digit == k ? 0 : costs[i]);
dp[i][1][k] = min(dp[i - 1][0][k], dp[i - 1][1][k ^ 1]) +
(cur_digit == k ? 0 : costs[i]);
}
}
cout << min(dp[N - 1][1][0], dp[N - 1][1][1]) << endl;
return 0;
}
```

0 comments on commit 48f9fee

Please sign in to comment.