Advertisement
Vince14

Untitled

Feb 14th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. #include <unordered_map>
  12. #include <numeric>
  13. #include <iomanip>
  14. using namespace std;
  15. #define pii pair<int, int>
  16. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  17. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  18. const int MOD = 1e9;
  19. const int MAX = 100005;
  20.  
  21.  
  22. int n;
  23. int cow[MAX];
  24. int loc[MAX];
  25.  
  26. struct fenwick{
  27. int arr[MAX];
  28. int size;
  29. void init(int p){
  30. size = p;
  31. }
  32. int query(int p) {
  33. int ret = 0;
  34. for (; p; p-=p&-p){
  35. ret += arr[p];
  36. }
  37. return ret;
  38. }
  39. void update(int p, int v) {
  40. for(; p < MAX; p+=p&-p){
  41. arr[p] += v;
  42. }
  43. }
  44. } a, b;
  45.  
  46. int main() {
  47. FAST;
  48. freopen("sleepy.in", "r", stdin);
  49. freopen("sleepy.out", "w", stdout);
  50. cin >> n;
  51. for(int i = 1; i <= n; i++){
  52. cin >> cow[i];
  53. loc[cow[i]] = n + 1 - i;
  54. }
  55.  
  56. a.init(n);
  57. b.init(n);
  58. int num_inversions = 0;
  59. int latest = 1e9;
  60. for(int i = 1; i <= n; i++){
  61. int q = a.query(loc[i]);
  62. // cout << i << " " << loc[i] << " " << q << "\n";
  63. if(q != 0){
  64. num_inversions += q;
  65. latest = min(latest, loc[i]);
  66. }
  67. a.update(loc[i], 1);
  68. }
  69. latest = n + 1 - latest;
  70. if(num_inversions == 0){
  71. cout << 0 << "\n";
  72. return 0;
  73. }
  74. cout << latest << "\n";
  75. for(int i = latest + 1; i <= n; i++){
  76. b.update(cow[i], 1);
  77. }
  78. for(int i = 1; i <= latest; i++){
  79. cout << latest - i + b.query(cow[i]);
  80. if(i != latest){
  81. cout << " ";
  82. }
  83. b.update(cow[i], 1);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement