树状数组模板
2017-10-20 17:57
176 查看
#include<iostream> #include<algorithm> #include<cstring> #include<string.h> #define ll long long using namespace std; const int N = 5010; int bit ; int n; void add(int i) { while (i <= n) { bit[i] += 1; i += (i&-i); } } int sum(int i) { int s = 0; while (i>0) { s += bit[i]; i -= (i&-i); } return s; } int main() { while (cin>>n) { ll result = 0; int a ; memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) { cin >> a[i],a[i]++; add(a[i]); result+= i - sum(a[i]); } ll ans = result; for (int i = 1; i <= n; i++) { result = result + n - 2 * a[i] + 1; ans = min(ans, result); } cout << ans << endl; } return 0; }
相关文章推荐
- 【模板】树状数组 区间修改,区间求和 (模板题:洛谷P3368树状数组2)
- nyoj-116 士兵杀敌(二)(树状数组插点问线-模板题)
- 树状数组模板
- c++树状数组2模板
- poj 1195 二维树状数组 及二维树状数组模板
- 树状数组 (模板)
- 树状数组模板
- 树状数组 BIT - 模板
- 【模板】树状数组 区间修改,区间求和 (模板题:洛谷P3372线段树1)
- 树状数组 线段树 模板
- poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)
- 树状数组模板
- 二维树状数组模板
- 树状数组模板
- HDU 1166 线段树模板&树状数组模板
- HDOJ 1556 Color the ball(树状数组模板)
- 树状数组模板程序
- 洛谷P3374 【模板】树状数组 1(CDQ分治)
- 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)
- 树状数组 线段树 模板