fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. int consistency1(int n){
  33.  
  34. int ans = 0;
  35.  
  36. for(int j=1; j<=n-2; j++){
  37.  
  38. int ct1 = 0;
  39. int i = j-1;
  40. while(i>=0){
  41. if(a[i] > a[j]) ct1++;
  42. i--;
  43. }
  44.  
  45. int ct2 = 0;
  46. int k = j+1;
  47. while(k<n){
  48. if(a[k] > a[j]) ct2++;
  49. k++;
  50. }
  51.  
  52. ans += (ct1 * ct2);
  53. }
  54.  
  55. return ans;
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. int consistency2(int n){
  67.  
  68. vector<int> pGreaterThan(n);
  69.  
  70. for(int i=1; i<n; i++){
  71. int ct = 0;
  72. int j = i-1;
  73. while(j>=0){
  74. if(a[j] > a[i]) ct++;
  75. j--;
  76. }
  77. pGreaterThan[i] = ct;
  78. }
  79.  
  80. vector<int> sSmallerThan(n);
  81.  
  82. for(int i=n-2; i>=0; i--){
  83. int ct = 0;
  84. int j = i+1;
  85. while(j<n){
  86. if(a[j] < a[i]) ct++;
  87. j++;
  88. }
  89. sSmallerThan[i] = ct;
  90. }
  91.  
  92.  
  93. int ans = 0;
  94. for(int j=1; j<=n-3; j++){
  95. for(int k=j+1; k<=n-2; k++){
  96. int x = a[j];
  97. int y = a[k];
  98.  
  99. if(x>=y) continue;
  100.  
  101. int ct1 = pGreaterThan[j];
  102. int ct2 = sSmallerThan[k];
  103.  
  104. ans += (ct1 * ct2);
  105. }
  106. }
  107.  
  108. return ans;
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. int practice(int n){
  143.  
  144.  
  145. return 0;
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. void solve() {
  153.  
  154. int n;
  155. cin>> n;
  156.  
  157. a.resize(n);
  158. for(int i=0; i<n; i++) cin >> a[i];
  159.  
  160. cout << consistency1(n) << " , Followup => " << consistency2(n) << endl;
  161.  
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. int32_t main() {
  170. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  171.  
  172. int t = 1;
  173. // cin >> t;
  174. while (t--) {
  175. solve();
  176. }
  177.  
  178. return 0;
  179. }
Success #stdin #stdout 0.01s 5292KB
stdin
7
8 3 6 1 7 2 5
stdout
17 , Followup => 13