HDU 1394 线段树or 树状数组~
Description
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.
Input
Output
Sample Input
Sample Output
| Memory: 320 KB | Time: 78 MS |
#include <cstdio>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = ;
int sum[maxn<<];
void PushUp(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
}
void build(int l,int r,int rt){
sum[rt] = ;
if(l == r) return ;
int m = (l + r) >> ;
build(lson);
build(rson);
return ;
}
void update(int p,int l,int r,int rt){
if(l == r){
sum[rt]++;
return ;
}
int m = (l + r)>>;
if(p <= m) update(p,lson);
else update(p,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L <= l&&r <= R){
return sum[rt];
}
int m = (l + r)>>;
int ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
}
int x[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
build(,n - ,);
int sum = ;
for(int i = ;i < n;i++){
scanf("%d",&x[i]);
sum += query(x[i],n - ,,n - ,);
update(x[i],,n - ,);
}
int res = sum;
for(int i = ;i < n;i++){
sum += n - x[i] - x[i] - ;
res = min(res,sum);
}
printf("%d\n",res);
}
return ;
}
后来我有用树状数组做了一下,发觉树状数组一般都要比线段树要快~
| Memory: 332 KB | Time: 31 MS |
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = ;
int c[maxn];
int a[maxn];
int n;
inline int lowbit(int x){
return x&(-x);
}
int sum(int x){
int ret = ;
while(x > ){
ret += c[x];x -= lowbit(x);
}
return ret;
}
void add(int x,int d){
while(x <= n){
c[x] += d;x += lowbit(x);
}
}
int main(){
while(~scanf("%d",&n)){
memset(c,,sizeof(c));
memset(a,,sizeof(a));
int ans = ;
for(int i = ;i <= n;i++){
scanf("%d",&a[i]);
ans += i - - sum(a[i]);
add(a[i] + ,);
}
//cout<<ans<<endl;
int MIN = ans;
for(int i = ;i <= n;i++){
MIN += n - (a[i] + ) - (a[i]);
ans = min(MIN,ans);
}
printf("%d\n",ans);
}
return ;
}
HDU 1394 线段树or 树状数组~的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)
题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...
- hdu 1394 线段树
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 2492 Ping pong (数状数组)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 1394 Minimum Inversion Number - 树状数组
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- hdu 1394(线段树) 最小逆序数
http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...
- hdu 1394 线段树计算逆序数
线段树计算逆序数的原理: 用线段树来统计已插入的数的个数(所以要保证最大的那个数不能太大,否则数组都开不了),然后每插入一个数,就查询比插入的数大的个数,累加即可. 这个题还有一个特点就是,题目给的是 ...
随机推荐
- Linux内核0.11体系结构 ——《Linux内核完全注释》笔记打卡
0 总体介绍 一个完整的操作系统主要由4部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如图0.1所示.操作系统内核程序主要用于对硬件资源的抽象和访问调度. 图0.1 操作系统组成部分 内核 ...
- MFC对话框使用CPrintDialog实现打印,指定打印机、后台打印
推荐下 不错. 对话框打印,网上一搜一大堆,基本分2类: A类: CPrintDialog.DoModal,然后在模态对话框里选打印机.打印配置: B类:GetPrinterDeviceDefault ...
- 集训第六周 数学概念与方法 计数 排列 L题
Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样. 话 ...
- Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...
- xcap发包工具的简单使用1(构造报文)
xcap是一个免费的网络发包工具,可以构造和发送常用的网络报文,如arp,ip,icmp,udp等,支持构造报文和发送报文等. 报文隶属于报文组,每个报文组包含多个报文,因此,创建报文首先要创建报文组 ...
- PHP读取mysql中的数据
<!DOCTYPE HTML> <html> <head> <title> PHP动态读取mysql中的数据 </title> <me ...
- 用C# ASP.net将数据库中的数据表导出到Excel中
需要用到组件GridView和一个button即可. 给GridView添加一个数据源, 选择你想要的数据库中的表的字段,添加成功后GridView中就显示数据. 再添加一个button,双击控件添加 ...
- topcoder SRM 639 div2
见这里 http://ygdtc.sinaapp.com/?p=257
- Linux下汇编语言学习笔记20 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- HDU1166 线段树裸题 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...