Advertisement
a53

Oposumi

a53
Jun 16th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream fin("oposumi.in");
  5. ofstream fout("oposumi.out");
  6.  
  7. const int INF = 2e9;
  8. const int N = 1000;
  9.  
  10. int a[1 + N][1 + N];
  11. bool FR[1 + N * N];
  12.  
  13. int SearchBLevel(int x, int n)
  14. {
  15. int st, dr, ans, mid;
  16. st = 1;
  17. dr = n;
  18. ans = n;
  19. while(st <= dr)
  20. {
  21. mid = (st + dr) >> 1;
  22. if((n * (n + 1) - (n - mid + 1) * (n - mid + 2)) / 2 + 1 >= x)
  23. {
  24. ans = mid;
  25. dr = mid - 1;
  26. }
  27. else
  28. st = mid + 1;
  29. }
  30.  
  31. return ans;
  32. }
  33. void Solution2()
  34. {
  35. int n, k, level;
  36. fin>>n>>k;
  37. level = SearchBLevel(k, n);
  38. a[level][level] = k;
  39. FR[k] = true;
  40. for(int i = n, j = n, cnt = n * (n + 1) / 2; i > level; cnt--)
  41. {
  42. a[i][j] = cnt;
  43. FR[cnt] = true;
  44. j--;
  45. if(j == level - 1)
  46. {
  47. i--;
  48. j = i;
  49. }
  50. }
  51. int cnt = 1;
  52. for(int i = 1; i <= n; i++)
  53. {
  54. for(int j = 1; j < min(level, i + 1); j++)
  55. {
  56. while(FR[cnt])
  57. ++cnt;
  58. a[i][j] = cnt;
  59. FR[cnt] = true;
  60. }
  61. }
  62. for(int i = 1; i <= n; i++)
  63. {
  64. for(int j = 1; j <= i; j++)
  65. fout<<a[i][j]<<" ";
  66. fout<<'\n';
  67. }
  68. }
  69.  
  70. int main()
  71. {
  72. int t;
  73. fin>>t;
  74. if(t == 1)
  75. {
  76. int n;
  77. fin>>n;
  78. for(int x = 1; x <= n * (n + 1) / 2; x++)
  79. fout<<SearchBLevel(x, n)<<' ';
  80. }
  81. else
  82. Solution2();
  83. return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement