《算法竞赛·快冲300题》将于2024年出版,是《算法竞赛》的辅助练习册。
所有题目放在自建的OJ New Online Judge。
用C/C++、Java、Python三种语言给出代码,以中低档题为主,适合入门、进阶。
“ 单峰数组” ,链接: http://oj.ecustacm.cn/problem.php?id=1839
题目描述
【题目描述】 本题对单峰数组的定义:
前一部分递增,后一部分递减,注意每部分可以为空,即完全递增、完全递减的数组也称为单峰数组。
现在给你一个长度为n的数组,每次可以交换相邻两个数字,请输出最小交换次数使得数组变成本题定义的单峰数组。
【输入格式】 输入第一行为正整数n,1≤n≤300000。
接下来n行,每行一个数字ai,表示数组a,1≤ai≤1000000000。
输入数字各不相同。
【输出格式】 输出一个数表示答案。
【输入样例】
8
7
4
8
10
1
2
6
9
【输出样例】
7
题解
如何操作才能以最小交换次数获得单峰数组?显然,最小的数字肯定要挪到最左边或最右边,那么应该挪到左边还是右边?为了使挪动次数最少,应该挪到交换次数少的那一边。最小的数字处理完,后面不用再管它。下一步挪第二小的数,同样挪到最左边或最右边。依次这样从小到大处理所有的数,直到结束。这样操作的交换次数肯定最少。
样例{7 4 8 10 1 2 6 9}的操作步骤如下:
(1)挪最小的1。挪到最左边需要交换4次,挪到最右边需要交换3次;取挪到右边的3次。新序列是{7 4 8 10 2 6 9 1},因为1不用再管,去掉1,新序列是{7 4 8 10 2 6 9}。
(2)挪新序列中最小的2。挪到最左边需4次,挪到最右边只需2次;取2次。去掉2,新序列是{7 4 8 10 6 9}。
…
依次操作,一共交换3+2+1+1+0+0+0+0=7次。
如果用暴力法做以上操作,挪动每个数是
O
(
n
)
O(n)
O(n)复杂度的,挪n个数是
O
(
n
2
)
O(n^2)
O(n2)的,超时。
本题用树状数组求解,代码简单、效率高,计算复杂度是
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)的。
用到的技术有离散化和树状数组,具体做法参考 《算法竞赛》“4.2.3 树状数组扩展应用”的“2. 偏序问题(逆序对 + 离散化)” ,几乎一样。
【笔记】 离散化、树状数组 。
C++代码
#include<bits/stdc++.h>
using namespace std;
const int N = 300000 + 10;
int a[N], olda[N], tree[N], pos[N];
#define lowbit(x) ((x) & - (x))
void update(int x, int d){ //修改元素a[x], a[x] = a[x] + d
while(x <= N)
tree[x] += d, x += lowbit(x);
}
int sum(int x){ //查询前缀和:返回前缀和sum = a[1] + a[2] +... + a[x]
int ans = 0;
while(x)
ans += tree[x], x -= lowbit(x);
return ans;
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i], olda[i] = a[i];
sort(olda + 1, olda + 1 + n);
for(int i = 1; i <= n; i++){
//离散化,得到新数组a[]
a[i] = lower_bound(olda + 1, olda + 1 + n, a[i]) - olda;
pos[a[i]] = i; //记录每个数字的idx
update(i, 1); //初始化树状数组:树状数组的下标是这里的idx
}
long long ans = 0;
for(int i = 1; i <= n; i++) {
int idx = pos[i];
int before = sum(idx - 1); //利用树状数组求before
int after = n - i - before;
ans += min(before, after); //贪心:挪到最左或最右
update(idx, -1); //更新树状数组
}
cout<<ans<<endl;
return 0;
}
Java代码
import java.util.*;
public class Main {
static final int N = 300000 + 10;
static int[] a = new int[N], olda = new int[N], tree = new int[N], pos = new int[N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
olda[i] = a[i];
}
Arrays.sort(olda, 1, n + 1);
for(int i = 1; i <= n; i++) {
//离散化,得到新数组a[]
a[i] = Arrays.binarySearch(olda, 1, n + 1, a[i]);
if(a[i] < 0) a[i] = -a[i] - 1;
pos[a[i]] = i; //记录每个数字的idx
update(i, 1); //初始化树状数组:树状数组的下标是这里的idx
}
long ans = 0;
for(int i = 1; i <= n; i++) {
int idx = pos[i];
int before = sum(idx - 1); //利用树状数组求before
int after = n - i - before;
ans += Math.min(before, after); //贪心:挪到最左或最右
update(idx, -1); //更新树状数组
}
System.out.println(ans);
}
static void update(int x, int d) { //修改元素a[x], a[x] = a[x] + d
while(x <= N) {
tree[x] += d;
x += lowbit(x);
}
}
static int sum(int x) { //查询前缀和:返回前缀和sum = a[1] + a[2] +... + a[x]
int ans = 0;
while(x > 0) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
static int lowbit(int x) {
return x & (-x);
}
}
Python代码
N = 300000 + 10
a, olda, tree, pos = [0] * N, [0] * N, [0] * N, [0] * N
def update(x, d): #修改元素a[x], a[x] = a[x] + d
global tree
while x <= N:
tree[x] += d
x += lowbit(x)
def sum(x): #查询前缀和:返回前缀和sum = a[1] + a[2] +... + a[x]
global tree
ans = 0
while x > 0:
ans += tree[x]
x -= lowbit(x)
return ans
def lowbit(x): return x & (-x)
n = int(input())
for i in range(1, n+1):
a[i] = int(input())
olda[i] = a[i]
olda = sorted(olda[1:n+1])
for i in range(1, n+1):
a[i] = olda.index(a[i]) + 1 # 离散化, 得到新数组a[]
pos[a[i]] = i # 记录每个数字的idx
update(i, 1) # 初始化树状数组:树状数组的下标是这里的idx
ans = 0
for i in range(1, n+1):
idx = pos[i]
before = sum(idx - 1) # 利用树状数组求before
after = n - i - before
ans += min(before, after) # 贪心:挪到最左或最右
update(idx, -1) # 更新树状数组
print(ans)