13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciexport class AccessFannkuch {
173af6ab5fSopenharmony_ci    public n: int;
183af6ab5fSopenharmony_ci    constructor() {
193af6ab5fSopenharmony_ci        this.n = 8;
203af6ab5fSopenharmony_ci    }
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_ci    static expected: int = 22;
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_ci    public fannkuch(n: int): int {
253af6ab5fSopenharmony_ci        // Not parsed new int[n]
263af6ab5fSopenharmony_ci        let perm: int[] = new int[this.n];
273af6ab5fSopenharmony_ci        // Not parsed new int[n]
283af6ab5fSopenharmony_ci        let perm1: int[] = new int[this.n];
293af6ab5fSopenharmony_ci        // Not parsed new int[n]
303af6ab5fSopenharmony_ci        let count: int[] = new int[this.n];
313af6ab5fSopenharmony_ci        // Not parsed new int[n]
323af6ab5fSopenharmony_ci        let maxPerm: int[] = new int[this.n];
333af6ab5fSopenharmony_ci        let maxFlipsCount: int = 0;
343af6ab5fSopenharmony_ci        let m: int = this.n - 1;
353af6ab5fSopenharmony_ci
363af6ab5fSopenharmony_ci        for (let i: int = 0; i < this.n; i++) perm1[i] = i;
373af6ab5fSopenharmony_ci        let r: int = this.n;
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci        while (true) {
403af6ab5fSopenharmony_ci            while (r != 1) {
413af6ab5fSopenharmony_ci                count[r - 1] = r;
423af6ab5fSopenharmony_ci                r--;
433af6ab5fSopenharmony_ci            }
443af6ab5fSopenharmony_ci            if (!(perm1[0] == 0 || perm1[m] == m)) {
453af6ab5fSopenharmony_ci                for (let i: int = 0; i < this.n; i++) perm[i] = perm1[i];
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_ci                let flipsCount: int  = 0;
483af6ab5fSopenharmony_ci                let k: int;
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_ci                while (!((k = perm[0]) == 0)) {
513af6ab5fSopenharmony_ci                let k2: int = (k + 1) >> 1;
523af6ab5fSopenharmony_ci                    for (let i = 0; i < k2; i++) {
533af6ab5fSopenharmony_ci                        let temp: int = perm[i];
543af6ab5fSopenharmony_ci                        perm[i] = perm[k - i];
553af6ab5fSopenharmony_ci                        perm[k - i] = temp;
563af6ab5fSopenharmony_ci                    }
573af6ab5fSopenharmony_ci                    flipsCount++;
583af6ab5fSopenharmony_ci                }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci                if (flipsCount > maxFlipsCount) {
613af6ab5fSopenharmony_ci                    maxFlipsCount = flipsCount;
623af6ab5fSopenharmony_ci                    for (let i = 0; i < this.n; i++) maxPerm[i] = perm1[i];
633af6ab5fSopenharmony_ci                }
643af6ab5fSopenharmony_ci            }
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci            while (true) {
673af6ab5fSopenharmony_ci                if (r == n) return maxFlipsCount;
683af6ab5fSopenharmony_ci                let perm0: int = perm1[0];
693af6ab5fSopenharmony_ci                let i: int = 0;
703af6ab5fSopenharmony_ci                while (i < r) {
713af6ab5fSopenharmony_ci                    let j: int = i + 1;
723af6ab5fSopenharmony_ci                    perm1[i] = perm1[j];
733af6ab5fSopenharmony_ci                    i = j;
743af6ab5fSopenharmony_ci                }
753af6ab5fSopenharmony_ci                perm1[r] = perm0;
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_ci                count[r] = count[r] - 1;
783af6ab5fSopenharmony_ci                if (count[r] > 0) break;
793af6ab5fSopenharmony_ci                r++;
803af6ab5fSopenharmony_ci            }
813af6ab5fSopenharmony_ci        }
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci
843af6ab5fSopenharmony_ci    public run(): void {
853af6ab5fSopenharmony_ci       let ret = this.fannkuch(this.n);
863af6ab5fSopenharmony_ci        assert ret == AccessFannkuch.expected: "Invalid result";
873af6ab5fSopenharmony_ci    }
883af6ab5fSopenharmony_ci}
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_cifunction main(): void {
913af6ab5fSopenharmony_ci    let a = new AccessFannkuch;
923af6ab5fSopenharmony_ci    a.run();
933af6ab5fSopenharmony_ci}
94