Created
September 15, 2018 07:18
-
-
Save completejavascript/48cb4726ede67cd8966668cae468fa51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
/* | |
* Hoán vị 2 số a và b | |
*/ | |
void swap(int &a, int &b) | |
{ | |
int temp = a; | |
a = b; | |
b = temp; | |
} | |
/* | |
* Sắp xếp dãy theo chiều tăng dần của dãy a | |
* Dãy b được đi kèm theo dãy a | |
* left, right: chỉ số trái, phải của dãy | |
*/ | |
void QuickSort_Ascending(int *a, int *b, int left, int right) | |
{ | |
int i = left, j = right; | |
int pivot = a[(i+j)/2]; | |
while(i <= j) | |
{ | |
while(a[i] < pivot) i++; | |
while(a[j] > pivot) j--; | |
if(i <= j) | |
{ | |
swap(a[i],a[j]); | |
swap(b[i],b[j]); | |
i++; | |
j--; | |
} | |
} | |
if(left < j) QuickSort_Ascending(a,b,left,j); | |
if(i < right) QuickSort_Ascending(a,b,i,right); | |
} | |
int main() | |
{ | |
ios::sync_with_stdio(false); | |
freopen("input.txt","r",stdin); | |
int T, N; | |
int height[1000]; // Lưu chiều cao của N người | |
int numTallerBef[1000]; // Lưu số người cao hơn đứng trước | |
int result[1000]; // Lưu dãy kết quả | |
cin >> T; | |
for(int i = 0; i < T; i++) | |
{ | |
// Nhập đầu vào | |
cin >> N; | |
for(int i = 0; i < N; i++) | |
cin >> height[i]; | |
for(int i = 0; i < N; i++) | |
{ | |
cin >> numTallerBef[i]; | |
result[i] = -1; | |
} | |
// Sắp xếp lại dãy theo chiều tăng dần của chiều cao | |
QuickSort_Ascending(height, numTallerBef, 0, N-1); | |
// Duyệt dãy sau khi sắp xếp để tìm vị trí đúng | |
int count, j, temp; | |
for(int i = 0; i < N; i++) | |
{ | |
temp = numTallerBef[i]; | |
j = count = 0; | |
if(temp == 0) | |
{ | |
for(j = 0; j < N; j++) | |
if(result[j] == -1) break; | |
result[j] = height[i]; | |
} | |
else | |
{ | |
for(j = 0; j < N; j++) | |
{ | |
if(result[j] == -1) count++; | |
if(count == temp && result[j+1] == -1) break; | |
} | |
result[j+1] = height[i]; | |
} | |
} | |
for(int i = 0; i< N; i++) | |
cout << result[i] << " "; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment