LGP1966

LGP1966

十月 28, 2021

LGP1966

Analysis

观察$\sum_{1}^{n}(a_i-b_i)^2$,展开发现$a_i^2,b_i^2$的总和是固定的,所以说为了满足题目条件,就应该让$\sum a_ib_i$尽可能大。由排序不等式可知,顺序和是最大的,所以说这道题就可以转换成一道逆序对数的题目

关于排序不等式戳这

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<bitset>
#include<set>
#include<cmath>
using namespace std;

#define rint register int
#define Int64 long long
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define clr(x,y) memset(x,y,sizeof(x))
#define sqar(x) (x)*(x)
#define lowbit(x) (x&(-x))
#define swp(x,y) x^=y,y^=x,x^=y
#define Maxn 100005
#define Mod 99999997

int N,ans;
struct dbk{
int val;
int pos;

bool operator <(const dbk &obj)const{
return val<obj.val;
}
}A[Maxn],B[Maxn];
int tmp[Maxn];

struct tree_array{
int v[Maxn];

void modify(int pos){
while(pos<=N){
v[pos]++;
pos+=lowbit(pos);
}
}

int query(int pos){
int res=0;
while(pos){
res+=v[pos];
res%=Mod;
pos-=lowbit(pos);
}
return res;
}
}T;

void sol(){
for(int i=1;i<=N;i++){
cin>>A[i].val;
A[i].pos=i;
}
for(int i=1;i<=N;i++){
cin>>B[i].val;
B[i].pos=i;
}
sort(A+1,A+1+N);
sort(B+1,B+1+N);
for(int i=1;i<=N;i++){
tmp[A[i].pos]=B[i].pos;
}
for(int i=1;i<=N;i++){
T.modify(tmp[i]);
ans+=(T.query(N)-T.query(tmp[i]));
ans%=Mod;ans+=Mod;ans%=Mod;
}
}

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
cin>>N;
sol();
cout<<ans;
return 0;
}