Last active
July 3, 2016 18:37
-
-
Save joaogui1/bbf18ccecc2c92a992461121204ea941 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
//Checar a posição lexicogŕafica de uma ordem de um vetor, O(n * n!) | |
int n, v[10], u[10], ans, stop; | |
cin >> n; | |
for(int i = 0; i < n; ++i){ | |
cin >> v[i]; //leitura, O(n) | |
} | |
for(int i = 0; i < n; ++i){ | |
u[i] = v[i]; //copio o vetor para outro, O(n) | |
} | |
sort(u, u + n) //ordenação, O(nlog(n)) | |
for(ans = 0; 1; ++ans){ | |
stop = 0; | |
for(int i = 0; i < n; ++i){ //checa se os vetores são iguais, O(n) | |
if(u[i] != v[i]) break; | |
if(i == n - 1) stop = 1; | |
} | |
if(stop){ | |
break; | |
} | |
next_permutation(u, u + n); //transforma o u na próxima ordenação do vetor na ordem lexicográfica, pode se repetir n! vezes | |
} | |
cout << ans << "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment