From fc18c9b0b0918f7789c935e884bba6c8e23fab4c Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 27 Sep 2022 21:58:09 +0800 Subject: [PATCH] 351B - Jeff and Furik https://codeforces.com/problemset/submission/351/173713009 --- Codeforces/351/B/B.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Codeforces/351/B/B.cpp diff --git a/Codeforces/351/B/B.cpp b/Codeforces/351/B/B.cpp new file mode 100644 index 00000000..6436698b --- /dev/null +++ b/Codeforces/351/B/B.cpp @@ -0,0 +1,43 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3005; + +int n, p[N], c[N], ans; + +int lowbit(int x) { + return x & -x; +} + +void add(int x, int y) { + for (; x <= n; x += lowbit(x)) c[x] += y; +} + +int sum(int x) { + int res = 0; + for (; x; x -= lowbit(x)) res += c[x]; + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> p[i]; + } + + for (int i = n; i; i--) { + ans += sum(p[i] - 1); + add(p[i], 1); + } + + cout << (ans * 2 - ans % 2) << endl; + + return 0; +}