HDU 1394 树状数组求逆序对
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13036 Accepted Submission(s): 7968
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 5005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int a[N]; int lowbit(int x){
return x&(-x);
} void solve(int val,int x){
while(x<=n){
a[x]+=val;
x+=lowbit(x);
}
} int sum(int x){
int ans=;
while(x>){
ans+=a[x];
x-=lowbit(x);
}
return ans;
} main()
{
int t, i, j, k;
while(scanf("%d",&n)==){
memset(a,,sizeof(a));
int ans=;
for(i=;i<=n;i++) {
scanf("%d",&b[i]);
b[i]++;
ans+=sum(n)-sum(b[i]);
solve(,b[i]);
}
int num=ans;
for(i=;i<=n;i++){
num+=n-b[i]-b[i]+;//sum(n)-sum(b[i])-sum(b[i]-1);
ans=min(ans,num);
}
printf("%d\n",ans);
}
}
HDU 1394 树状数组求逆序对的更多相关文章
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))
传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)
对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
随机推荐
- Java中HashMap案例
package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class MapTest { pu ...
- Python学习笔记6-字典
定义 使用键值对, >>> person = {"name":"keven","age":15,"gender& ...
- 关于level DB的相关资料
可以参考: http://blog.csdn.net/houzengjiang/article/details/7718548 http://www.cnblogs.com/haippy/archiv ...
- 使用==比较String类型
String类型的比较 public class StringDemo { public static void main(String[] args) { String s1 = "abc ...
- UEditor文档
UEditor文档http://fex.baidu.com/ueditor/Ueditor 前后端数据交互 http://blog.csdn.net/bobo_93/article/details/5 ...
- 20160816_Redis一些资料
1.官网 http://redis.io/ 2.一个教程 http://www.yiibai.com/redis/redis_quick_guide.html 3.快速开始指南(Quick Start ...
- Lua a and b or c
lua中nil和false为条件不成立,其余都为条件成立. a and b : a条件不成立,则返回a,否则,返回b a or c : a条件成立,则返回a,否则,返回b 常用x = x or v ...
- The Daligner Overlap Library
/************************************************************************************\ * * * Copyrig ...
- Element can be click when out of view
WebDriver can't action the element when out of view Webdriver can't action the element when the elem ...
- 024-ActionResult解说
ActionResult是一个抽象类,是Action运行后的回传类型,但是当Action回传ActionResult的时候,其实并不包含这个ActionResult的运行结果,而是包含运行这个Acti ...