Submission #62279213
Source Code Expand
Copy
def solve():N=int(input())A=input().strip()n3=3**Nbits=[0]*n3for i,c in enumerate(A):bits[i]=0 if c=='0' else 1B=bits[:]length=n3for _ in range(N):new_length=length//3newB=[0]*new_lengthidx=0for i in range(new_length):s=B[idx]+B[idx+1]+B[idx+2]newB[i]=1 if s>=2 else 0idx+=3B=newBlength=new_lengthfinal_bit=B[0]
def solve(): N=int(input()) A=input().strip() n3=3**N bits=[0]*n3 for i,c in enumerate(A): bits[i]=0 if c=='0' else 1 B=bits[:] length=n3 for _ in range(N): new_length=length//3 newB=[0]*new_length idx=0 for i in range(new_length): s=B[idx]+B[idx+1]+B[idx+2] newB[i]=1 if s>=2 else 0 idx+=3 B=newB length=new_length final_bit=B[0] cost0=[0]*n3 cost1=[0]*n3 for i in range(n3): if bits[i]==0: cost0[i]=0 cost1[i]=1 else: cost0[i]=1 cost1[i]=0 length=n3 for _ in range(N): new_length=length//3 new_cost0=[0]*new_length new_cost1=[0]*new_length idx=0 for i in range(new_length): c0_0=cost0[idx] c0_1=cost0[idx+1] c0_2=cost0[idx+2] c1_0=cost1[idx] c1_1=cost1[idx+1] c1_2=cost1[idx+2] s00=c0_0+c0_1+c0_2 s01=c0_0+c0_1+c1_2 s02=c0_0+c1_1+c0_2 s03=c1_0+c0_1+c0_2 new_cost0[i]=min(s00,s01,s02,s03) s10=c1_0+c1_1+c1_2 s11=c1_0+c1_1+c0_2 s12=c1_0+c0_1+c1_2 s13=c0_0+c1_1+c1_2 new_cost1[i]=min(s10,s11,s12,s13) idx+=3 cost0, cost1=new_cost0, new_cost1 length=new_length if final_bit==0: print(cost1[0]) else: print(cost0[0]) solve()
Submission Info
Submission Time | |
---|---|
Task | E - Hierarchical Majority Vote |
User | tamakiiiiii |
Language | Python (PyPy 3.10-v7.3.12) |
Score | 450 |
Code Size | 1598 Byte |
Status | AC |
Exec Time | 188 ms |
Memory | 152552 KB |
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 | 54 ms | 76576 KB |
00_sample_02.txt | AC | 54 ms | 76288 KB |
01_random_01.txt | AC | 79 ms | 82340 KB |
01_random_02.txt | AC | 185 ms | 136656 KB |
01_random_03.txt | AC | 55 ms | 76484 KB |
01_random_04.txt | AC | 184 ms | 136544 KB |
01_random_05.txt | AC | 76 ms | 84036 KB |
01_random_06.txt | AC | 144 ms | 152552 KB |
01_random_07.txt | AC | 102 ms | 89336 KB |
01_random_08.txt | AC | 186 ms | 139352 KB |
01_random_09.txt | AC | 54 ms | 76392 KB |
01_random_10.txt | AC | 186 ms | 135356 KB |
01_random_11.txt | AC | 55 ms | 76252 KB |
01_random_12.txt | AC | 121 ms | 151872 KB |
01_random_13.txt | AC | 55 ms | 76212 KB |
01_random_14.txt | AC | 188 ms | 136276 KB |
01_random_15.txt | AC | 72 ms | 81632 KB |
01_random_16.txt | AC | 182 ms | 148076 KB |
01_random_17.txt | AC | 79 ms | 82340 KB |
01_random_18.txt | AC | 183 ms | 135564 KB |
01_random_19.txt | AC | 54 ms | 76588 KB |
01_random_20.txt | AC | 184 ms | 135512 KB |
02_handmade_01.txt | AC | 129 ms | 139172 KB |
02_handmade_02.txt | AC | 129 ms | 139224 KB |
02_handmade_03.txt | AC | 121 ms | 151656 KB |
02_handmade_04.txt | AC | 135 ms | 135252 KB |
02_handmade_05.txt | AC | 132 ms | 139196 KB |
02_handmade_06.txt | AC | 136 ms | 135404 KB |
02_handmade_07.txt | AC | 132 ms | 135544 KB |
02_handmade_08.txt | AC | 128 ms | 139016 KB |
02_handmade_09.txt | AC | 132 ms | 135704 KB |
02_handmade_10.txt | AC | 119 ms | 151980 KB |