| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1330352 | bradley0927 | Just Long Neckties 2 (JOI25_ho_t4) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
// State: counts of each value 1..21, encoded as __int128
// 5 bits per value * 21 values = 105 bits, fits in __int128
const int MAXV = 21;
typedef __int128 State;
State encode(int cnt[]) {
State s = 0;
for (int i = MAXV; i >= 1; i--) {
s = (s << 5) | cnt[i];
}
return s;
}
void decode(State s, int cnt[]) {
for (int i = 1; i <= MAXV; i++) {
cnt[i] = s & 31;
s >>= 5;
}
}
int respond(int cnt[], int x) {
// find largest tail <= x, replace with x
for (int v = x; v >= 1; v--) {
if (cnt[v] > 0) {
cnt[v]--;
cnt[x]++;
return 0; // no new model needed
}
}
// no tail <= x found, need new model starting at 1, set to x
cnt[x]++;
return 1; // new model added
}
int countModels(int cnt[]) {
int total = 0;
for (int i = 1; i <= MAXV; i++) total += cnt[i];
return total;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
// set of (prev_skipped, state)
set<pair<int, State>> curr, next;
// initial state: no models, prev not skipped
int init[MAXV + 1] = {};
curr.insert({0, encode(init)});
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
next.clear();
for (auto &[prev, s] : curr) {
int cnt[MAXV + 1];
decode(s, cnt);
// case 1: ignore (only if prev not skipped)
if (!prev) {
next.insert({1, s});
}
// case 2: respond
int cnt2[MAXV + 1];
copy(cnt, cnt + MAXV + 1, cnt2);
respond(cnt2, v[i]);
next.insert({0, encode(cnt2)});
}
curr = next;
}
for (auto &[prev, s] : curr) {
int cnt[MAXV + 1];
decode(s, cnt);
ans = min(ans, countModels(cnt));
}
cout << ans << endl;
}