题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

题意:给出一个循环数组,求其逆序对最少为多少;

思路:对于逆序对: 交换两个相邻数,逆序数 +1 或 -1, 交换两个不相邻数 a, b, 逆序数 += 两者间大于 a 的个数 - 两者间小于 a 的个数;

所以只要求出初始时的逆序对数,就可以推出其余情况时的逆序对数.对于求初始逆序对数,这里 n 只有 5e3,可以直接暴力 / 树状数组 / 线段树 / 归并排序;

代码:

1.直接暴力

 #include <iostream>
#include <stdio.h>
using namespace std; const int MAXN = 5e3 + ;
int a[MAXN]; int main(void){
int n, ans = ;
while(~scanf("%d", &n)){
ans = ;
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
for(int j = ; j < i; j++){
if(a[j] > a[i]) ans++;
}
}
int cnt = ans;
for(int i = ; i < n; i++){
cnt += (n - a[i] - ) - a[i];
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return ;
}

2.树状数组

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = 5e3 + ;
int tree[MAXN], a[MAXN], n; int lowbit(int x){
return x & (-x);
} void updata(int x, int d){
while(x <= n){
tree[x] += d;
x += lowbit(x);
}
} int sum(int x){
int ans = ;
while(x > ){
ans += tree[x];
x -= lowbit(x);
}
return ans;
} int main(void){
int ans = ;
while(~scanf("%d", &n)){
ans = ;
memset(tree, , sizeof(tree));
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
updata(a[i] + , );
ans += i - sum(a[i] + );//当前是第 i 个数,减去a[i]前面(这里包括了a[i])的数就是a[i]后面的数了,即可以和a[i]组成逆序对的数的数目
// ans += sum(n) - sum(a[i] + 1);
}
int cnt = ans;
for(int i = ; i <= n; i++){
cnt += (n - a[i] - ) - a[i];
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return ;
}

3.线段树

 #include <iostream>
#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 5e3 + ;
int sum[MAXN << ], a[MAXN]; void push_up(int rt){
sum[rt] = sum[rt << ] + sum[rt << | ];
} void build(int l, int r, int rt){
sum[rt] = ;
if(l == r) return;
int mid = (l + r) >> ;
build(lson);
build(rson);
} void update(int p, int x, int l, int r, int rt){
if(l == r){
sum[rt] += x;
return;
}
int mid = (l + r) >> ;
if(p <= mid) update(p, x, lson);
else update(p, x, rson);
push_up(rt);
} int query(int L, int R, int l, int r, int rt){
if(l >= L && r <= R) return sum[rt];
int mid = (l + r) >> ;
int ans = ;
if(L <= mid) ans += query(L, R, lson);
if(R > mid) ans += query(L, R, rson);
return ans;
} int main(void){
int n, ans = ;
while(~scanf("%d", &n)){
ans = ;
build(, n-, );
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
ans += query(a[i], n - , , n - , );
update(a[i], , , n - , );
}
int cnt = ans;
for(int i = ; i < n; i++){
cnt += (n - a[i] - ) - a[i];
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return ;
}

hdu1394(枚举/树状数组/线段树单点更新&区间求和)的更多相关文章

  1. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  2. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  3. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. HDU 3303 Harmony Forever 前缀和+树状数组||线段树

    Problem Description We believe that every inhabitant of this universe eventually will find a way to ...

  5. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 「CodePlus 2017 11 月赛」Yazid 的新生舞会(树状数组/线段树)

    学习了新姿势..(一直看不懂大爷的代码卡了好久T T 首先数字范围那么小可以考虑枚举众数来计算答案,设当前枚举到$x$,$s_i$为前$i$个数中$x$的出现次数,则满足$2*s_r-r > 2 ...

  7. 树状数组 && 线段树

    树状数组 支持单点修改 #include <cstdio> using namespace std; int n, m; ], c[]; int lowbit(int x) { retur ...

  8. hdu1166 敌兵布阵 树状数组/线段树

    数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...

  9. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

随机推荐

  1. [容易]Fizz Buzz 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/fizz-buzz/

  2. shell的面试题

    在网上看到一道考shell的题--统计出一个文件夹下大小大于7MB的文件夹 好,我也来装一下深沉,个人觉得,这道题主要考的是对经常使用的linux的熟练程度(我不是很熟,哈哈),题目要统计,主要用到两 ...

  3. IOS 十六进制字符串转换成UIColor

    /** * 十六进制转换成UIColor * * @param stringToConvert 十六进制字符串 * * @return UIColor */ +(UIColor *) hexStrin ...

  4. WIFI模块ESP8266的使用指南【转】

    本文转载自:http://www.itdadao.com/articles/c15a814052p0.html 本文主要对讲述ESP8266模块硬件连接工作,以及作为服务器和客户端情况下的配置实现的详 ...

  5. poj2965 The Pilots Brothers' refrigerator —— 技巧性

    题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...

  6. 分享知识-快乐自己:MySQL中的约束,添加约束,删除约束,以及一些其他修饰

    创建数据库: CREATE DATABASES 数据库名: 选择数据库: USE 数据库名: 删除数据库: DROP DATAVBASE 数据库名: 创建表: CREATE TABLE IF NOT ...

  7. PHPexcel把数据库数据直接转化为excel表格

    运行文件  index.php <?php$dir =dirname(__FILE__); //获取当前文件的路径require $dir.'/Classes/phpexcel.php'; // ...

  8. Hotel California

    On a dark desert highway行驶在昏黑的荒漠公路上cool wind in my hair凉风吹过我的头发warm smell of colutas温馨的大麻香rising up ...

  9. listen and translation exercise 53

    It was hard work and there weren't any interesting things for him. You should be an expert with comp ...

  10. hdu-5806 NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接: NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/13107 ...