Submission #62295125
Source Code Expand
Copy
#include <bits/stdc++.h>using namespace std;typedef long long ll;int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int N;string A;cin >> N >> A;// 長さは3^Nint L = 1;for (int i = 0; i < N; i++) L *= 3;// シミュレーションで元の最終多数決結果を得るvector<int> cur(L);for (int i = 0; i < L; i++){cur[i] = A[i]-'0';}int tempL = L;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
string A;
cin >> N >> A;
// 長さは3^N
int L = 1;
for (int i = 0; i < N; i++) L *= 3;
// シミュレーションで元の最終多数決結果を得る
vector<int> cur(L);
for (int i = 0; i < L; i++){
cur[i] = A[i]-'0';
}
int tempL = L;
for (int lvl = 0; lvl < N; lvl++){
int newL = tempL/3;
vector<int> nxt(newL,0);
for (int i = 0; i < newL; i++){
int sum = cur[3*i] + cur[3*i+1] + cur[3*i+2];
nxt[i] = (sum >= 2 ? 1 : 0);
}
cur = nxt;
tempL = newL;
}
int f = cur[0]; // 元の最終結果
int target = 1 - f;
// dp[level][i][bit] : 最小反転数(葉レベルが0, 以降は内部ノード)
// レベル0: 各リーフについて
vector<array<int,2>> dp(L);
for (int i = 0; i < L; i++){
int bit = A[i] - '0';
dp[i][0] = (bit==0?0:1);
dp[i][1] = (bit==1?0:1);
}
// 下からあげてdpを計算(各レベル毎にグループ3つ)
int curSize = L;
for (int lvl = 1; lvl <= N; lvl++){
int newSize = curSize / 3;
vector<array<int,2>> newdp(newSize);
for (int i = 0; i < newSize; i++){
// 各グループについて 子は dp[3*i], dp[3*i+1], dp[3*i+2]
for (int b = 0; b < 2; b++){
int base = 0;
vector<int> diffs;
for (int k = 0; k < 3; k++){
int cost_b = dp[3*i+k][b];
int cost_other = min(dp[3*i+k][b], dp[3*i+k][1-b]);
base += cost_other;
diffs.push_back(cost_b - cost_other);
}
sort(diffs.begin(), diffs.end());
int cnt_zero = 0;
for (int d : diffs) {
if(d==0) cnt_zero++;
}
int extra = 0;
if(cnt_zero >= 2){
extra = 0;
} else if(cnt_zero == 1){
// 1個が既に b としているため,あと1個必要
// diffs[0]は0,次に小さい正の値を選ぶ
extra = diffs[cnt_zero];
} else { // cnt_zero==0
extra = diffs[0] + diffs[1];
}
newdp[i][b] = base + extra;
}
}
dp = move(newdp);
curSize = newSize;
}
cout << dp[0][target] << "\n";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Hierarchical Majority Vote |
| User | una_o0 |
| Language | C++ 20 (gcc 12.2) |
| Score | 450 |
| Code Size | 2725 Byte |
| Status | AC |
| Exec Time | 120 ms |
| Memory | 28400 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 450 / 450 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 1 ms | 3516 KiB |
| 00_sample_02.txt | AC | 1 ms | 3480 KiB |
| 01_random_01.txt | AC | 2 ms | 3604 KiB |
| 01_random_02.txt | AC | 118 ms | 28308 KiB |
| 01_random_03.txt | AC | 1 ms | 3376 KiB |
| 01_random_04.txt | AC | 117 ms | 28400 KiB |
| 01_random_05.txt | AC | 5 ms | 4124 KiB |
| 01_random_06.txt | AC | 114 ms | 28236 KiB |
| 01_random_07.txt | AC | 14 ms | 6056 KiB |
| 01_random_08.txt | AC | 118 ms | 28264 KiB |
| 01_random_09.txt | AC | 1 ms | 3456 KiB |
| 01_random_10.txt | AC | 118 ms | 28312 KiB |
| 01_random_11.txt | AC | 1 ms | 3444 KiB |
| 01_random_12.txt | AC | 107 ms | 28300 KiB |
| 01_random_13.txt | AC | 1 ms | 3512 KiB |
| 01_random_14.txt | AC | 118 ms | 28180 KiB |
| 01_random_15.txt | AC | 1 ms | 3540 KiB |
| 01_random_16.txt | AC | 118 ms | 28172 KiB |
| 01_random_17.txt | AC | 2 ms | 3692 KiB |
| 01_random_18.txt | AC | 120 ms | 28288 KiB |
| 01_random_19.txt | AC | 1 ms | 3440 KiB |
| 01_random_20.txt | AC | 119 ms | 28368 KiB |
| 02_handmade_01.txt | AC | 108 ms | 28292 KiB |
| 02_handmade_02.txt | AC | 110 ms | 28324 KiB |
| 02_handmade_03.txt | AC | 106 ms | 28164 KiB |
| 02_handmade_04.txt | AC | 115 ms | 28296 KiB |
| 02_handmade_05.txt | AC | 109 ms | 28316 KiB |
| 02_handmade_06.txt | AC | 115 ms | 28292 KiB |
| 02_handmade_07.txt | AC | 114 ms | 28396 KiB |
| 02_handmade_08.txt | AC | 109 ms | 28244 KiB |
| 02_handmade_09.txt | AC | 114 ms | 28304 KiB |
| 02_handmade_10.txt | AC | 107 ms | 28324 KiB |