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 = + ;
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 = ;
for (int i = x; i > ; 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 = ; i <= n; i++) {
int u; scanf("%d", &u); u++;
a[i] = u;
}
int tot = ;
for (int i = n; i >= ; i--){
tot += sum(a[i]);
add(a[i], );
}
///l->r的区间
///区间内比他大的
for (int i = n; i > ; i--){
memset(tree, , sizeof(tree));
for (int j = n; j >= i;j--){///都取不到边界
if (a[j] > a[i]) add(a[j], );
big[i][j] = sum(n) - sum(a[i] - );
}
} ///r->l的区间
///区间内比他大的
for (int i = ; i <= n; i++){
memset(tree, , sizeof(tree));
for (int j = ; j <= i; j++){
if (a[j] > a[i]) add(a[j], );
big2[i][j] = sum(n) - sum(a[i] - );
}
} int mintot = tot;
int cnt = ;
for (int i = ; i <= n; i++){///left
for (int j = i + ; j <= n; j++){///right
if (a[i] < a[j]) continue;
int t1 = * (big[i][i + ] - big[i][j]) - (j - (i + ));
int t2 = j - (i + ) - * (big2[j][j - ] - big2[j][i]);
int tmp = tot + t1 + t2 - ;
if (tmp < mintot) cnt = , mintot = tmp;
else if (tmp == mintot) cnt++;
}
}
printf("%d %d\n", mintot, cnt);
return ;
}

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

  1. CF 313 DIV2 B 树状数组

    http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数 ...

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

    题目和南阳那道题一样链接http://www.cnblogs.com/zzuli2sjy/p/4943774.html 代码: 1 #include<stdio.h> 2 #include ...

  3. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  4. Codeforce 101B. Buses(线段树or树状数组+离散化)

     Buses                                                                                               ...

  5. Codeforces #528 Div2 F (1087F) Rock-Paper-Scissors Champion 树状数组+set

    题意:n个人站成一排,初始时刻每个人手中都有一个图案,可能是石头,剪刀,布3个中的1种,之后会随机选取相邻的两个人玩石头剪刀布的游戏,输的人会离开(如果两个人图案相同,则随机选择一个人离开).执行(n ...

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

    Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...

  7. EC R 87 div2 D. Multiset 线段树 树状数组 二分

    LINK:Multiset 主要点一下 二分和树状数组找第k大的做法. 线段树的做法是平凡的 开一个数组实现就能卡过. 考虑如树状数组何找第k大 二分+查询来判定是不优秀的. 考虑树状数组上倍增来做. ...

  8. 【魔改】树状数组 牛客多校第五场I vcd 几何+阅读理解

    https://www.nowcoder.com/acm/contest/143/I vc-dimension 题解:分三种情况,组合数学算一下,其中一种要用树状数组维护 技巧(来自UESTC):1. ...

  9. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

随机推荐

  1. 第一节windows系统安装虚拟机VMware 软件

    1,在windows系统下,下载虚拟机软件VMware-workstation-full-10.0.1 2,解压虚拟机软件,安装虚拟机 第一个为虚拟机注册机,第二个为虚拟机安装软件 点击虚拟机安装软件 ...

  2. js--闭包的理解

    从技术上来讲,在JS中,每个function都是闭包,因为它总是能访问在它外部定义的数据. 当该内部函数在外部函数外被调用,就生成了闭包. 函数内部可以直接读取全局变量. 闭包就是能够读取其他函数内部 ...

  3. Google科学家前腾讯副总裁吴军将出席第二届万物互联创新大会

    当越来越多的科技产品注入互联网的基因,"万物互联"的模式悄然兴起.第二届万物互联创新大会(B12大会)将于2016-11-13日在杭州市余杭区隆重召开.Google科学家前腾讯副总 ...

  4. 【C++】最大子列和

    此题来自<数据结构与算法>,书中一共介绍了四种方法,这里贴出两种. 1.分治递归,对本题来说,虽然有更好的算法,但是用此题理解分治算法感觉挺有用 #include <iostream ...

  5. 字符串匹配—KMP 扩展KMP Manacher

    kuangbin字符串专题传送门--http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview 算法模板: KMP: ; ...

  6. 浙大pat 1062题解

    1061. Dating (20) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Sherlock Holmes ...

  7. html中的js监听付款按钮--触发ajax调用php后台--得到的json数据---交给安卓原生处理

    //01 var pay_status = new Object();$(".sure_pay").on("touchstart",function(){ va ...

  8. 判断括号字符串是否为合法+求n对括号的所有组合

    n对括号的有效组合数 参考:https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0 import java.util.Ar ...

  9. webservice整合spring cxf

    下载cxf包,把他里面的包都添加进lib文件夹中. 创建一个接口.添加@WebService注解 @WebService public interface HelloWorld { String sa ...

  10. Cash Machine

    Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...