Advertisement
a53

catmin

a53
Feb 4th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. ///O(n*(log(n)+sigma))
  2. #include <fstream>
  3. using namespace std;
  4. ifstream fin("catmin.in");
  5. ofstream fout("catmin.out");
  6. int n,i,m,j,c,nh;
  7. char b[1000002];
  8. int r,x,heapMin[100002];
  9. char a[12];
  10. void urc(int f){
  11. while(f>1 && heapMin[f/2]>heapMin[f]){
  12. int aux=heapMin[f/2];
  13. heapMin[f/2]=heapMin[f];
  14. heapMin[f]=aux;
  15. f=f/2;
  16. }
  17. }
  18. void cobor(int p, int nh){
  19. while(p*2<=nh){
  20. int r=p*2;
  21. if(r+1<=nh && heapMin[r+1]<heapMin[r]){ r=r+1; }
  22. if(heapMin[p]>heapMin[r]){
  23. int aux=heapMin[r];
  24. heapMin[r]=heapMin[p];
  25. heapMin[p]=aux;
  26. p=r;
  27. }
  28. else{
  29. break;
  30. }
  31. }
  32. }
  33. int main(){
  34. fin>>n; nh=0;
  35. for(i=1;i<=n;i++){///O(n)
  36. fin>>a;///citesc numarul ca sir de cifre
  37. x=0;///construiesc numarul
  38. ///O(sigma)
  39. for(r=0;a[r]!=0;r++){ x=x*10+(a[r]-'0'); }
  40. m=m+r;///numarul total de cifre
  41. ///completez cu cifre de 9
  42. while(r<9){x=x*10+9; r++;}
  43. ///adaug in heap-min
  44. nh++; heapMin[nh]=x;
  45. ///O(log(n))
  46. urc(nh);
  47. }
  48.  
  49. r=100000000;
  50. for(i=0;i<=m-1;i++){///O(n*sigma)
  51. ///extrag din heap prima cifra de la cel mai mic numar
  52. b[i]='0'+heapMin[1]/r;
  53. ///tai prima cifra si completez cu 9
  54. heapMin[1]=(heapMin[1]%r)*10+9;
  55. ///actualizez pozitia in heap
  56. ///O(log(n))
  57. cobor(1,nh);
  58. }
  59. b[m]=0;
  60. fout<<b;
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement