PATA1064题解

xiaoxiao2021-02-28  41

// // main.cpp // PATA1064 // // Created by Phoenix on 2018/2/16. // Copyright © 2018年 Phoenix. All rights reserved. // #include <cstdio> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 1010; int n, num[maxn], in[maxn]; int bin[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; void level(int left, int right, int index) { if(index > n) return; int k = right - left + 1, t = -1; for(int i = 0; i < 11; i++){ if(k < bin[i]){ t = bin[i-1]; break; } } int temp; if(k >= t && k < t + t / 2){ temp = k - t / 2; }else{ temp = t - 1; } in[index] = left + temp; level(left, left + temp - 1, 2 * index); level(left + temp + 1, right, 2 * index + 1); } int main(int argc, const char * argv[]) { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &num[i]); } sort(num + 1, num + n + 1); level(1, n, 1); for(int i = 1; i <= n; i++) { printf("%d", num[in[i]]); if(i < n) printf(" "); else printf("\n"); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2600314.html

最新回复(0)