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. // 7
  28. // 8 3 6 1 7 2 5
  29.  
  30.  
  31.  
  32.  
  33. vector<int> a;
  34.  
  35. int consistency1(int n){
  36.  
  37. int ans = 0;
  38.  
  39. for(int j=1; j<=n-2; j++){
  40.  
  41. int ct1 = 0;
  42. int i = j-1;
  43. while(i>=0){
  44. if(a[i] > a[j]) ct1++;
  45. i--;
  46. }
  47.  
  48. int ct2 = 0;
  49. int k = j+1;
  50. while(k<n){
  51. if(a[k] > a[j]) ct2++;
  52. k++;
  53. }
  54.  
  55. ans += (ct1 * ct2);
  56. }
  57.  
  58. return ans;
  59.  
  60. }
  61.  
  62.  
  63.  
  64. //* Followup :
  65.  
  66. int consistency2(int n){
  67.  
  68. int ans = 0;
  69.  
  70. for(int j=1; j<=n-3; j++){
  71. for(int k=j+1; k<=n-2; k++){
  72.  
  73. if(a[j] >= a[k]) continue;
  74.  
  75. int ct1 = 0;
  76. int i = j-1;
  77. while(i>=0){
  78. if(a[i] > a[j]) ct1++;
  79. i--;
  80. }
  81.  
  82. int ct2 = 0;
  83. int l = k+1;
  84. while(l<=n-1){
  85. if(a[l] < a[k]) ct2++;
  86. l++;
  87. }
  88.  
  89. ans += ct1*ct2;
  90.  
  91. }
  92. }
  93.  
  94. return ans;
  95. }
  96.  
  97.  
  98.  
  99.  
  100. int consistency3(int n){
  101.  
  102. int ans = 0;
  103.  
  104. vector<int> pCountGreaterThan(n, 0);
  105. for(int j=1; j<=n-1; j++){
  106. int i = j-1;
  107. while(i>=0){
  108. if(a[i] > a[j]) pCountGreaterThan[j]++;
  109. i--;
  110. }
  111. }
  112.  
  113. vector<int> sCountSmallerThan(n, 0);
  114. for(int k=0; k<=n-2; k++){
  115. int l = k+1;
  116. while(l<=n-1){
  117. if(a[l] < a[k]) sCountSmallerThan[k]++;
  118. l++;
  119. }
  120. }
  121.  
  122. for(int j=1; j<=n-3; j++){
  123. for(int k=j+1; k<=n-2; k++){
  124.  
  125. if(a[j] >= a[k]) continue;
  126.  
  127. ans += pCountGreaterThan[j] * sCountSmallerThan[k];
  128.  
  129. }
  130. }
  131.  
  132. return ans;
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. int practice(int n){
  155.  
  156.  
  157. return 0;
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164. void solve() {
  165.  
  166. int n;
  167. cin>> n;
  168.  
  169. a.resize(n);
  170. for(int i=0; i<n; i++) cin >> a[i];
  171.  
  172. cout << consistency1(n) << endl;
  173. cout << "Followup => " << consistency2(n) << " " << consistency3(n) << endl;
  174.  
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182. int32_t main() {
  183. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  184.  
  185. int t = 1;
  186. // cin >> t;
  187. while (t--) {
  188. solve();
  189. }
  190.  
  191. return 0;
  192. }
Success #stdin #stdout 0.01s 5284KB
stdin
7
8 3 6 1 7 2 5
stdout
17
Followup => 13 13