CF618F. Double Knapsack(高考袭击)
codeforces原题
洛谷翻译
思路:
需要证明一个结论:
对于 A , B A, B A,B 集合的任意排列,都会有满足条件的子段解
即:对于原始给定的排列,两序列总有不一定相同长度的字段和一定
接下来证明这个结论:
设
p
r
e
a
i
prea_i
preai 表示
A
A
A 序列前
i
i
i 个的和,
p
r
e
b
i
preb_i
prebi 表示
B
B
B 序列前
i
i
i 个的和。
且
p
r
e
a
n
≤
p
r
e
b
n
prea_n ≤ preb_n
prean≤prebn。
设
c
i
c_i
ci 为 满足
p
r
e
a
i
≥
p
r
e
b
j
prea_i ≥ preb_j
preai≥prebj 的最大的
j
j
j
则可以列出不等式:
p
r
e
a
i
≤
p
r
e
b
c
i
+
1
prea_i ≤ preb_{c_i+1}
preai≤prebci+1
即:
p
r
e
a
i
≤
p
r
e
b
c
i
+
b
c
i
+
1
prea_i ≤ preb_{c_i} + b_{c_i+1}
preai≤prebci+bci+1
移项得:
p
r
e
a
i
−
p
r
e
b
c
i
≤
b
c
i
+
1
prea_i-preb_{c_i} ≤ b_{c_i+1}
preai−prebci≤bci+1
又因为
c
i
c_i
ci 为下表,
所以有:
1
≤
c
i
+
1
≤
n
1 ≤ c_i+1 ≤ n
1≤ci+1≤n
将此带入上述移项后不等式,得:
0
≤
p
r
e
a
i
−
p
r
e
b
c
i
≤
n
0 ≤ prea_i-preb_{c_i} ≤ n
0≤preai−prebci≤n,有
n
n
n 个解
但是因为
i
i
i 是下表,
所有有:
0
≤
i
≤
n
0 ≤ i ≤ n
0≤i≤n,有
n
+
1
n+1
n+1 个解
根据抽屉原理,可以得出:
两个序列中,总有 p r e a i − p r e b c i = p r e a j − p r e b c j prea_i - preb_{c_i}=prea_j-preb_{c_j} preai−prebci=preaj−prebcj
证毕。
最后,只需要找相等就够了。