Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <vector>
int main(){
const long B = 1e5 + 7;
std::vector<long> a(B, 0);
long n; scanf("%ld", &n);
long cnt(0);
std::vector<long> v(n, 0); for(long p = 0; p < n; p++){scanf("%ld", &v[p]); ++a[v[p]]; cnt = (cnt > a[v[p]]) ? cnt : a[v[p]];}
bool possible(true);
for(long p = 2; p < B; p++){if(a[p - 1] < a[p]){possible = false; break;}}
if(possible){
printf("%ld\n", cnt);
for(long p = 0; p < n; p++){printf("%ld ", a[v[p]]); --a[v[p]];};
puts("");
}
else{puts("-1");}
return 0;
}
Copy The Code &
Try With Live Editor
Input
9
1 2 3 1 2 1 4 2 5
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
3 1 2 1 2 2 2 3 2
Demonstration
Codeforces Solution-D. Permutations-Solution in C, C++, Java, Python