C
遍历,根据每个线段最左和最右不断“缩小”,最终得到答案
D
在原始牌和新牌中挑出N张最大,重新组成数组
排序后双指针
E
x y分开计算
任取一对点x1,x2x_1,x_2x1,x2,记d为两个点的x差值,剩下可以随意取k-2个点,每一个组合都在整体的cost中贡献d
取ddd从1…n-1进行统计,每个点对有m*m个取法
∴cost=(k−2n∗m−2)∗m2∗∑d=1n−1(d∗(n−d))
\therefore cost= \binom{k - 2}{n * m - 2} * m^2 * \sum_{d=1}^{n-1}(d * (n - d))
∴cost=(n∗m−2k−2)∗m2∗d=1∑n−1(d∗(n−d))
组合数用逆元计算
F
绝对值加法,动态维护中位数
用两个堆
注意在python中a.append(x)会破坏a的堆性质
之后再使用heapq.heappush(a,x)是不会维护前面的数据的
我喜欢维护大堆长度+1>=小堆长度>=大堆长度
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n = int(fp.readline())
ls, rs = 0, 0
l, r = [], []
ans = 0
for i in range(n):
items = list(map(int, fp.readline().split()))
if items[0] == 1:
a, b = items[1:]
ans += b
if len(r) == 0:
heapq.heappush(l, -a)
ls += a
else:
topl, topr = -l[0], r[0]
if a < topl:
heapq.heappush(l, -a)
ls += a
elif a > topr:
heapq.heappush(r, a)
rs += a
else:
heapq.heappush(l, -a)
ls += a
if len(l) - len(r) > 1:
tl = -l[0]
heapq.heappop(l)
heapq.heappush(r, tl)
ls -= tl
rs += tl
elif len(r) > len(l):
tr = r[0]
heapq.heappop(r)
heapq.heappush(l, -tr)
rs -= tr
ls += tr
if items[0] == 2:
if len(l) == 0:
print(-l[0], ans)
else:
print(-l[0], ans + (-l[0]) * len(l) - ls + rs - (-l[0] * len(r)))
if __name__ == "__main__":
main()
文章讲述了在Atcoder编程挑战中,如何通过C遍历和动态维护堆(如大顶堆和小顶堆)来解决涉及线段、最大值选取和成本计算的问题,特别提到了绝对值加法和维护中位数的方法。
259

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



