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 = + ;
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 树状数组的更多相关文章
- CF 313 DIV2 B 树状数组
http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数 ...
- codeforce 597C-Subsequences(dp+树状数组)
题目和南阳那道题一样链接http://www.cnblogs.com/zzuli2sjy/p/4943774.html 代码: 1 #include<stdio.h> 2 #include ...
- 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
- Codeforce 101B. Buses(线段树or树状数组+离散化)
Buses ...
- Codeforces #528 Div2 F (1087F) Rock-Paper-Scissors Champion 树状数组+set
题意:n个人站成一排,初始时刻每个人手中都有一个图案,可能是石头,剪刀,布3个中的1种,之后会随机选取相邻的两个人玩石头剪刀布的游戏,输的人会离开(如果两个人图案相同,则随机选择一个人离开).执行(n ...
- CF #261 div2 D. Pashmak and Parmida's problem (树状数组版)
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...
- EC R 87 div2 D. Multiset 线段树 树状数组 二分
LINK:Multiset 主要点一下 二分和树状数组找第k大的做法. 线段树的做法是平凡的 开一个数组实现就能卡过. 考虑如树状数组何找第k大 二分+查询来判定是不优秀的. 考虑树状数组上倍增来做. ...
- 【魔改】树状数组 牛客多校第五场I vcd 几何+阅读理解
https://www.nowcoder.com/acm/contest/143/I vc-dimension 题解:分三种情况,组合数学算一下,其中一种要用树状数组维护 技巧(来自UESTC):1. ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
随机推荐
- 分布式版本控制系统Git-----8.fst-forward与no fast foward
当前分支合并到另一分支时,如果没有分歧解决,就会直接移动文件指针.这个过程叫做fastforward. 举例来说,开发一直在master分支进行,但忽然有一个新的想法,于是新建了一个develop的分 ...
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- noip2015Day2T2-子串
题目描述 Description 有两个仅包含小写英文字母的字符串A和B.现在要从字符串A中取出k个互不重叠的非空子串,然后把这k个子串按照其在字符串A中出现的顺序依次连接起来得到一个新的字符串,请问 ...
- 数据库 Mysql内容补充一
mysql时间函数 --获取当前日期 select current_date(); --获取当前时间 select current_time(); --获取当前的日期和时间 select now(); ...
- DD应用实例
1.将本地的/dev/hdb整盘备份到/dev/hdd dd if=/dev/hdb of=/dev/hdd2.将/dev/hdb全盘数据备份到指定路径的image文件dd if=/dev/hdb o ...
- hdu_5324_Boring Class(cdq分治+树状数组)
题目链接:hdu_5324_Boring Class 题意: 给出n个二维点对,求LIS长度和编号字典序最小的LIS(x非增,y非减) 题解: dp[i]=max(dp[j]) (i>j,l[i ...
- deque (STL)
//双端队列 //deque的成员函数 c.assign(beg, end); //将[beg, end]区间中的数据赋值给c c.assign(n, elem); //将n个elem的拷贝赋值给c ...
- Openjudge-计算概论(A)-能被3,5,7整除的数
描述: 输入一个整数,判断它能否被3,5,7整除,并输出以下信息:1.能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格):2.能被其中两个数整除(输出两个数,小的在前,大的在后.例如:3 ...
- SharePoint2013基于Form(FBA)的AD认证登陆
来源于:http://www.haogongju.net/art/1964313 1. 使用SharePoint2013实现基于AD的Form认证,首先创建一个Web Application,步骤如下 ...
- web上传大文件的配置
1.项目本身的webconfig 在<system.web>字段下 <httpRuntime targetFramework="4.5" requestLeng ...