PAT顶级题解目录
Larry just studied the algorithm to count number of inversions. He’s very interested in it. He’s considering another problem: Given a permutation of integers from 1 to n, how many inversions it has if we reverse one of its subarray?
Formally speaking, given an integer array a (indices are from 0 to n−1) which contains a permutation of integers from 1 to n, two elements a[i] and a[j] form an inversion if a[i]>a[j] and i<j. Your job is to count, for each pair of 0≤i≤j<n, the number of inversions if we reverse the subarray from a[i] to a[j].
Input Specification:
Each input file contains one test case. Each case consists of a positive integer n (≤1,000) in the first line, and a permutation of integers from 1 to n in the second line. The numbers in a line are separated by a single space.
Output Specification:
For each test case, output n(n+1)/2 integers in a single line. The results are for reversing subarray indicating by all possible pairs of indices 0≤i≤j<n in i-major order – that is, the first n results are for the reverse of subarrary [0…0], [0…1], …[0…n−1]; the next n−1 results are for the reverse of subarry [1…1], [1…2],…, [1…n−1] and so on.
All the numbers in a line must be separated by a single space, with no extra space at the beginning or the end of the line.
Sample Input:
3
2 1 3
Sample Output:
1 0 2 1 2 1
Hint:
The original array is { 2, 1, 3 }.
Reversing subarray [0…0] makes { 2, 1, 3 } which has 1 inversion.
Reversing subarray [0…1] makes { 1, 2, 3 } which has 0 inversion.
Reversing subarray [0…2] makes { 3, 1, 2 } which has 2 inversions.
Reversing subarray [1…1] makes { 2, 1, 3 } which has 1 inversion.
Reversing subarray [1…2] makes { 2, 3, 1 } which has 2 inversions.
Reversing subarrays [2…2] makes { 2, 1, 3 } which has 1 inversion.
题目大意:
求给出一串序列,求其逆序数。但是并不是仅仅求当前这个序列的逆序数,对这串逆序数进行排列组合,输出各个组合的逆序数,顺序按照原序列,进行[0…0], [0…1], …[0…n−1],[1…1], [1…2],…, [1…n−1],…, [n-1…n-1],进行逆置。([i…j]表示将序列i位置到j位置进行逆置)。
解题思路:
最暴力的方法就是对每次组合后的序列求其逆序数。很显然,为了降低时间,会想到只是进行一些逆序操作,原序列的逆序数与逆置后的序列的逆序数之间是不是有关系。别说,还真有。
记序列(
a
1
,
a
2
,
a
3
.
.
.
,
a
n
a_{1}, a_{2}, a_{3}...,a_{n}
a1,a2,a3...,an)的逆序数为
S
S
S
其子序列(
a
i
,
.
.
.
,
a
j
a_{i}, ... ,a_{j}
ai,...,aj))的逆序数为
T
T
T
则原序列
[
i
,
j
]
[i,j]
[i,j] 逆置后序列(
a
1
,
.
.
.
.
,
a
j
−
1
,
a
j
,
.
.
.
.
,
a
i
+
1
,
a
i
,
.
.
.
.
,
a
n
a_{1}, ...., a_{j-1},a_{j}, ...., a_{i+1}, a_{i}, ...., a_{n}
a1,....,aj−1,aj,....,ai+1,ai,....,an)的逆序数为
S
−
2
∗
T
+
(
j
−
i
)
∗
(
j
−
i
+
1
)
/
2
S - 2 * T + (j-i) * (j-i+1) / 2
S−2∗T+(j−i)∗(j−i+1)/2
现在的问题就成了能不能以尽量少的重复运算计算
T
T
T
设置一个二维数组num,大小为n*n, num[i][j]表示在原序列中因为
a
i
a_{i}
ai的原因而在
i
−
j
i-j
i−j的位置产生的逆序数。先要求子序列(
a
i
,
.
.
.
,
a
j
a_{i}, ... ,a_{j}
ai,...,aj))的逆序数
T
T
T的话,我们即求num[i][j] + num[i+1][j] + … +num[j][j]
代码:
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
scanf("%d", &n);
vector<int>v(n);
vector<vector<int>> num(n, vector<int>(n, 0));
for(int i = 0; i < n; ++ i)
scanf("%d", &v[i]);
int S = 0; #原序列的逆序数
for(int i = 0; i < n; ++ i){
int temp = 0;
for(int j = i; j < n; ++ j){
if(v[j] < v[i])
++ temp;
num[i][j] = temp;
}
S += temp;
}
for(int i = 0; i < n; ++ i)
for(int j = i; j < n; ++ j){
if(i == j){
if(i != 0)
printf(" "); #第一个数字前不需要空格
printf("%d", S);
}
else{
int T = 0;
#求T大小
for(int t = i; t < j; ++ t)
T += num[t][j];
printf(" %d", S - 2 * T + (j-i) * (j-i+1) / 2);
}
}
}