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. HDU 5784 How Many Triangles

    计算几何,极角排序,双指针,二分. 直接找锐角三角形的个数不好找,可以通过反面来求解. 首先,$n$个点最多能组成三角形个数有$C_n^3$个,但是这之中还包括了直角三角形,钝角三角形,平角三角形,我 ...

  2. CSU 1640 机智的刷题方式

    完全背包 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  3. Bullcow 牡牛和牝牛(bzoj 3398)

    Description     约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡牛,也可以是牝牛.牛们要站成一排.但是牡牛是好斗的,为了避免牡牛闹出乱子,约翰决定任意两只牡 ...

  4. RabbitMQ持久化编码注意事项

    以Java语言,MQ客户端为amqp-client作为示例 1.基本原则 direct模式,由生产者声明队列名,消费者也声明队列名 topic模式,由生产者声明交换器名,由消费者声明队列名+交换器名+ ...

  5. Simple python reverse shell

    import base64,sys; import socket,struct s=socket.socket(2,socket.SOCK_STREAM) s.connect(('Attack's I ...

  6. 主题: jQuery异步调用KindEditor无法赋值【解决】

    KindEditor.ready(function (K) { window.editor1 = K.create('#tjssjs', { width: '480px', height: '150p ...

  7. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  8. Zeppelin使用phoenix解释器

    Interpreters设置

  9. JQ N级导航

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. jQuery版本冲突解决办法

    <!-- 引入1.6.4版的jq --> <script src="<a href="http://ajax.googleapis.com/ajax/lib ...