codeforce div2 C 树状数组

http://codeforces.com/contest/362

题目大意:给你一个序列,用冒泡排序法让他变为非递减的序列最少需要几次。在冒泡交换之间,你有一个swap操作,该swap操作是交换任意两个数组元素的位置,问在该操作后,所再需要的冒泡交换次数是多少,并输出方案数

思路:树状数组维护一下区间序列,知道该区间内比他大的有几个就行了。然后暴力。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int maxn = 5000 + 5;
int tree[maxn], a[maxn];
int big[maxn][maxn], big2[maxn][maxn];
int n;
int lowbit(int x) {return x & -x;}

int sum(int x){
    int ans = 0;
    for (int i = x; i > 0; i -= lowbit(i)){
        ans += tree[i];
    }
    return ans;
}

void add(int x, int val){
    for (int i = x; i <= n; i += lowbit(i)){
        tree[i] += val;
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int u; scanf("%d", &u); u++;
        a[i] = u;
    }
    int tot = 0;
    for (int i = n; i >= 1; i--){
        tot += sum(a[i]);
        add(a[i], 1);
    }
    ///l->r的区间
///区间内比他大的
    for (int i = n; i > 0; i--){
        memset(tree, 0, sizeof(tree));
        for (int j = n; j >= i;j--){///都取不到边界
            if (a[j] > a[i]) add(a[j], 1);
            big[i][j] = sum(n) - sum(a[i] - 1);
        }
    }

    ///r->l的区间
///区间内比他大的
    for (int i = 1; i <= n; i++){
        memset(tree, 0, sizeof(tree));
        for (int j = 1; j <= i; j++){
            if (a[j] > a[i]) add(a[j], 1);
            big2[i][j] = sum(n) - sum(a[i] - 1);
        }
    }

    int mintot = tot;
    int cnt = 0;
    for (int i = 1; i <= n; i++){///left
        for (int j = i + 1;  j <= n; j++){///right
            if (a[i] < a[j]) continue;
            int t1 = 2 * (big[i][i + 1] - big[i][j]) - (j - (i + 1));
            int t2 = j - (i + 1) - 2 * (big2[j][j - 1] - big2[j][i]);
            int tmp = tot + t1 + t2 - 1;
            if (tmp < mintot) cnt = 1, mintot = tmp;
            else if (tmp == mintot) cnt++;
        }
    }
    printf("%d %d\n", mintot, cnt);
    return 0;
}

时间: 2024-10-26 16:36:45

codeforce div2 C 树状数组的相关文章

CF 313 DIV2 B 树状数组

http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数组的本质就行了. 当然用sum[r]-sum[l]也是可以的

codeforce 597C-Subsequences(dp+树状数组)

题目和南阳那道题一样链接http://www.cnblogs.com/zzuli2sjy/p/4943774.html 代码: 1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<iostream> 6 #include<map> 7 typedef long long ll; 8 const ll

CF #261 div2 D. Pashmak and Parmida&amp;#39;s problem (树状数组版)

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that consists of n

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi

Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

弱弱的战壕 描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒[email protected][email protected]). 但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT).这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队. 战壕都有一个保护范围,同它的攻击

Hdu5032 极角排序+树状数组

题目链接 思路:参考了题解.对询问进行极角排序,然后用树状数组维护一下前缀和即可. /* ID: onlyazh1 LANG: C++ TASK: test */ #include<bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=1010; const int maxm=10

Curious Robin Hood(树状数组+线段树)

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri