fork download
  1. #include<bits/stdc++.h>
  2. #define f1(i, n) for(ll i=1;i<=n;++i)
  3. #define f0(i, n) for(ll i=0;i<n;++i)
  4. #define ull unsigned long long
  5. #define ll long long
  6. #define rev(a) reverse(a.begin(),a.end())
  7. #define all(x) x.begin(),x.end()
  8. #define so(A, n) sort(A+1, A+n+1)
  9. using namespace std;
  10. const int maxn = 200010;
  11. const int N = 1e6 + 1;
  12. int main()
  13. {
  14. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  15. int n;
  16. cin >> n;
  17. if (n <= 1e3) {
  18. int A[n + 1];
  19. f1(i, n) {
  20. cin >> A[i];
  21. }
  22. int cnt = 0;
  23. while (1) {
  24. bool check = false;
  25. for (int i = 1; i < n; ++i) {
  26. if (A[i] > A[i + 1]) {
  27. swap(A[i], A[i + 1]);
  28. for (int j = 1; j <= n; ++j) cout << A[j] << " ";
  29. cout << endl;
  30. check = true;
  31. cnt++;
  32. }
  33. }
  34. if (!check) break;
  35. }
  36. cout << cnt;
  37. }
  38. else
  39. {
  40. pair<int, int> A[n + 1];
  41. f1(i, n) {
  42. cin >> A[i].first;
  43. A[i].second = i;
  44. }
  45. sort(A + 1, A + n + 1);
  46. set<int> se;
  47. int res = 0;
  48. se.insert(A[1].second);
  49. for (int i = 2; i <= n; ++i) {
  50. set<int>::iterator it = se.upper_bound(A[i].second);
  51. res += abs(int(se.size() - distance(se.begin(), it)));
  52. se.insert(A[i].second);
  53. }
  54. cout << res;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63.  
Success #stdin #stdout 0.08s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty