本文涉及知识点
P1966 [NOIP 2013 提高组] 火柴排队
题目背景
NOIP2013 提高组 D1T2
题目描述
涵涵有两盒火柴,每盒装有 n n n 根火柴,每根火柴都有一个高度。 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:$ \sum (a_i-b_i)^2$。
其中 a i a_i ai 表示第一列火柴中第 i i i 个火柴的高度, b i b_i bi 表示第二列火柴中第 i i i 个火柴的高度。
每列火柴中相邻两根火柴的位置都可以交换,请你通过交换使得两列火柴之间的距离最小。请问得到这个最小的距离,最少需要交换多少次?如果这个数字太大,请输出这个最小交换次数对 1 0 8 − 3 10^8-3 108−3 取模的结果。
输入格式
共三行,第一行包含一个整数 n n n,表示每盒中火柴的数目。
第二行有 n n n 个整数,每两个整数之间用一个空格隔开,表示第一列火柴的高度。
第三行有 n n n 个整数,每两个整数之间用一个空格隔开,表示第二列火柴的高度。
输出格式
一个整数,表示最少交换次数对 1 0 8 − 3 10^8-3 108−3 取模的结果。
输入输出样例 #1
输入 #1
4
2 3 1 4
3 2 1 4
输出 #1
1
输入输出样例 #2
输入 #2
4
1 3 4 2
1 7 2 4
输出 #2
2
说明/提示
输入输出样例说明一
最小距离是 $ 0$,最少需要交换 1 1 1 次,比如:交换第 $1 $ 列的前 $ 2$ 根火柴或者交换第 2 2 2 列的前 $2 $ 根火柴。
输入输出样例说明二
最小距离是 10 10 10,最少需要交换 2 2 2 次,比如:交换第 1 1 1 列的中间 2 2 2 根火柴的位置,再交换第 2 2 2 列中后 2 2 2 根火柴的位置。
数据范围
对于 10 % 10\% 10% 的数据, 1 ≤ n ≤ 10 1 \leq n \leq 10 1≤n≤10;
对于 30 % 30\% 30% 的数据, 1 ≤ n ≤ 100 1 \leq n \leq 100 1≤n≤100;
对于 60 % 60\% 60% 的数据, 1 ≤ n ≤ 1 0 3 1 \leq n \leq 10^3 1≤n≤103;
对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105, 0 ≤ a i , b i < 2 31 0 \leq a_i,b_i < 2^{31} 0≤ai,bi<231 且对于任意 1 ≤ i < j ≤ n 1\le i<j\le n 1≤i<j≤n, a i ≠ a j a_i\neq a_j ai=aj, b i ≠ b j b_i\neq b_j bi=bj。
逆序对 树状树状
性质一:令
a
m
是
a
的最小值,
b
n
是
b
的最小值
a_m是a的最小值,b_n是b的最小值
am是a的最小值,bn是b的最小值。则
a
m
和
b
n
a_m和b_n
am和bn对应优于其它方案。
某方案:
a
m
对应
b
p
,
a
q
对应
b
n
a_m对应b_p,a_q对应b_n
am对应bp,aq对应bn,新方案:
a
m
对应
b
n
,
a
q
对应
b
p
a_m对应b_n,a_q对应b_p
am对应bn,aq对应bp。
旧方案这两组距离:
a
m
2
−
2
a
m
×
b
p
+
b
p
2
+
a
q
2
−
2
a
q
×
b
n
+
b
n
2
a_m^2-2 a_m\times b_p + b_p^2+a_q^2-2a_q\times b_n+b_n2
am2−2am×bp+bp2+aq2−2aq×bn+bn2
新方案这两组距离:
a
m
2
−
2
a
m
×
b
n
+
b
n
2
+
a
q
2
−
2
a
q
×
b
p
+
b
p
2
a_m^2-2a_m\times b_n+ b_n^2 +a_q^2- 2 a_q \times b_p + b_p^2
am2−2am×bn+bn2+aq2−2aq×bp+bp2
式子一=(旧方案-新方案)
÷
2
=
a
m
×
b
n
+
a
q
×
b
p
−
a
m
×
b
p
−
a
q
×
b
n
\div 2=a_m \times b_n + a_q \times b_p - a_m \times b_p - a_q \times b_n
÷2=am×bn+aq×bp−am×bp−aq×bn
令
a
q
=
a
m
+
x
,
b
p
=
b
n
+
y
。
x
>
0
,
y
>
0
a_q = a_m + x,b_p=b_n+y。x>0,y>0
aq=am+x,bp=bn+y。x>0,y>0
式子一=
a
m
×
b
n
+
(
a
m
+
x
)
×
(
b
n
+
y
)
−
a
m
×
(
b
n
+
y
)
−
(
a
m
+
x
)
×
b
n
=
x
×
y
>
0
a_m \times b_n + (a_m+x)\times(b_n+y) - a_m \times(b_n + y) -(a_m+x)\times b_n =x\times y>0
am×bn+(am+x)×(bn+y)−am×(bn+y)−(am+x)×bn=x×y>0。某方案劣于新方案。
性质二:
a
m
对应
b
n
a_m 对应b_n
am对应bn后,删除
a
m
,
b
n
a_m,b_n
am,bn继续迭代。即最小对应最小,次小对应次小
⋯
\cdots
⋯
性质三:我们只关心a(b)的相对大小,故可以离散化。
性质四:离散化后。 i对应i,j对应j。故同时将a和b的i、j互换,不影响结果。
实现
对a、b离散化。
mCode[a[i]]=i。
b[i] =mCode[b[i]]。
答案就是b的逆序对数量。
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
template<class ELE = int >
class ITreeArrSumOpe
{
public:
virtual void Assign(ELE& dest, const ELE& src) = 0;
virtual ELE Back(const ELE& n1, const ELE& n2) = 0;
};
template<class ELE = int >
class CTreeArrAddOpe :public ITreeArrSumOpe<ELE>
{
public:
virtual void Assign(ELE& dest, const ELE& src) {
dest += src;
}
virtual ELE Back(const ELE& n1, const ELE& n2) {
return n1 - n2;
}
};
template<class ELE = int, class ELEOpe = CTreeArrAddOpe<ELE> >
class CTreeArr
{
public:
CTreeArr(int iSize) :m_vData(iSize + 1)
{
}
void Add(int index, ELE value)
{
if ((index < 0) || (index >= m_vData.size() - 1)) { return; }
index++;
while (index < m_vData.size())
{
m_ope.Assign(m_vData[index], value);
index += index & (-index);
}
}
ELE Sum(int index)//[0...index]之和
{
index++;
ELE ret = 0;
while (index)
{
m_ope.Assign(ret, m_vData[index]);
index -= index & (-index);
}
return ret;
}
ELE Sum() { return Sum(m_vData.size() - 2); }
ELE Get(int index)
{
return m_ope.Back(Sum(index), Sum(index - 1));
}
private:
ELEOpe m_ope;
vector<ELE> m_vData;
};
template<class T = int>
class CDiscretize //离散化
{
public:
CDiscretize(vector<T> nums)
{
sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
m_nums = nums;
for (int i = 0; i < nums.size(); i++)
{
m_mValueToIndex[nums[i]] = i;
}
}
int operator[](const T value)const
{
auto it = m_mValueToIndex.find(value);
if (m_mValueToIndex.end() == it)
{
return -1;
}
return it->second;
}
int size()const
{
return m_mValueToIndex.size();
}
vector<T> m_nums;
protected:
unordered_map<T, int> m_mValueToIndex;
};
class Solution {
public:
int Ans(vector<int>& A, vector<int>& B) {
const int N = A.size();
CDiscretize<int>disA(A), disB(B);
for (auto& n : A) {
n = disA[n];
}
for (auto& n : B) {
n = disB[n];
}
unordered_map<int, int> mCode;
for (int i = 0;i < N;i++) {
mCode[A[i]] = i;
}
for (auto& n : B) {
n = mCode[n];
}
CTreeArr<int> bit(N);
long long ans = 0;
for (const auto& n : B) {
ans += bit.Sum() - bit.Sum(n);
bit.Add(n, 1);
}
return ans % (100'000'000 - 3);
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
int n;
cin >> n;
auto A = Read<int>(n);
auto B = Read<int>(n);
#ifdef _DEBUG
/*printf("T=%d,", T);*/
Out(A, "A=");
Out(B, "B=");
#endif // DEBUG
auto res = Solution().Ans(A,B);
cout << res << endl;
return 0;
}
单元测试
vector<int> A,B;
TEST_METHOD(TestMethod11)
{
A = { 2,3,1,4 },B = { 3,2,1,4 };
auto res = Solution().Ans(A,B);
AssertEx(1, res);
}
TEST_METHOD(TestMethod12)
{
A = { 1,3,4,2 },B = { 1,7,2,4 };
auto res = Solution().Ans(A, B);
AssertEx(2, res);
}

扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 员工说:技术至上,老板不信;投资人的代表说:技术至上,老板会信。 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步优快云学院,听白银讲师(也就是鄙人)的讲解。
https://edu.youkuaiyun.com/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.youkuaiyun.com/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。


被折叠的 条评论
为什么被折叠?



