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 ...
随机推荐
- System.Diagnostics.Process 执行.EXE
分类: C#+WINFORM 2009-04-05 21:09 459人阅读 评论(0) 收藏 举报 我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打 ...
- linux反弹shell
参考链接 http://www.cnblogs.com/r00tgrok/p/reverse_shell_cheatsheet.html http://www.waitalone.cn/linux-s ...
- [妙味 DOM] 第二课:DOM、BOM相关方法及属性
知识点总结 获取样式.增加样式.删除样式函数的封装 表格 tHead tBodies tFoot rows 行 cells 列 表单 表单可以通过name来获取元素:表单.name值 onchange ...
- POJ 2305 Basic remains(进制转换)
题目链接:http://poj.org/problem?id=2305 ime Limit: 1000MS Memory Limit: 65536K Total Submissions: 5326 ...
- C++静态成员
类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动. 静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: 类体中 ...
- 2016弱校联盟十一专场10.2——Around the World
题目链接:Around the World 题意: 给你n个点,有n-1条边,现在这n-1条边又多增加了ci*2-1条边,问你有多少条欧拉回路 题解: 套用best定理 Best Theorem:有向 ...
- hdu_5810_Balls and Boxes(打表推公式)
题目链接:hdu_5810_Balls and Boxes 题意: 如题,让你求那个公式的期望 题解: 打表找规律,然后推公式.这项技能必须得学会 #include<cstdio> #in ...
- Android常用Permission
位置相关: android.permission.WRITE_GSERVICES 允许程序修改Google服务地图(Allows an application to modify the Google ...
- Android onConfigurationChanged的作用
API原文说明: android:configChangesLists configuration changes that the activity will handle itself. When ...
- WildMagic 简单图元(一)
#include <Wm5WindowApplication3.h> #include <Wm5VisualEffectInstance.h> using namespace ...