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