AtCoder abc 144

数组操作与优化问题:两两相乘积、边界条件和动态规划,
文章讲述了三个编程题目:D-WaterBottlex涉及计算水的面积和体积,E-Gluttony要求找到数组两两相乘积不超过限制的最大值,F-ForkintheRoad讨论了在给定图中删除一条边以优化期望值的问题。文章使用Python和相关库解决这些问题,运用了排序、二分查找和动态规划策略。

D - Water Bottle
x先除以a,得到面积。体积和面积是等同考虑的。
分两种情况,一种是水比一半面积少,一种是水比一半面积多。

# -*- 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 sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    a, b, x = map(int, fp.readline().split())
    x /= a
    if x >= 0.5 * a * b:
        y = 2 * (a * b - x) / a
        t = (math.atan2(y, a) / math.pi) * 180
        print(t)
    else:
        y = 2 * x / b
        t = (math.atan2(y, b) / math.pi) * 180
        print(90 - t)


if __name__ == "__main__":
    main()

E - Gluttony
等价于求两个数组
a1,a2...anb1,b2...bna_1,a_2...a_n \\ b_1,b_2...b_na1,a2...anb1,b2...bn
求数组元素的两两相乘积最大值不能超过L的解法,并求L的最小值。
当你看到求数组某函数最大值的最小值,并且毫无头绪的时候,就应该想到二分。

元素两两相乘的积很明显的应该从a正序,b倒序进行相乘,此时可以保证L最小。如果不满足性质,那么将a扣掉份额。最后累计要扣掉的份额,计算是否满足题目需求。

# -*- 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 sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    n, k = map(int, fp.readline().split())
    a = list(map(int, fp.readline().split()))
    b = list(map(int, fp.readline().split()))
    a.sort()
    b.sort(reverse=True)

    def check(x):
        ret = 0
        for i in range(n):
            if a[i] * b[i] <= x:
                continue
            ret += a[i] - x // b[i]
        return ret <= k

    lo, hi = 0, 10 ** 13
    while lo < hi:
        mi = (lo + hi) // 2
        if check(mi):
            hi = mi
        else:
            lo = mi + 1
    print(lo)


if __name__ == "__main__":
    main()

F - Fork in the Road
普通不删点的dp不难想到。
N=600下,枚举每条可能删的边会超时。但是由于只需要删一条边,因此针对每个点,只可能是它期望值最大的子节点和它之间的那条边被删掉。
因此暴力求解即可。

# -*- 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 sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    n, m = map(int, fp.readline().split())
    g = [[] for _ in range(n)]
    f = [0] * n
    a = [0] * n

    def calc(pt: int):
        mx, sel = -1, -1
        if len(g[pt]) == 1:
            return 1e18
        for i in g[pt]:
            if a[i] > mx:
                mx, sel = a[i], i
        for i in range(n - 2, -1, -1):
            f[i] = 0
            c = 0
            for j in g[i]:
                if i == pt and j == sel:
                    continue
                f[i] += f[j]
                c += 1
            f[i] = f[i] / c + 1
        return f[0]

    for i in range(m):
        u, v = map(int, fp.readline().split())
        u, v = u - 1, v - 1
        g[u].append(v)

    for i in range(n - 2, -1, -1):
        a[i] = 0
        for j in g[i]:
            a[i] += a[j]
        a[i] = a[i] / len(g[i]) + 1
    ans = a[0]
    for i in range(n - 1):
        ans = min(ans, calc(i))
    print(ans)


if __name__ == "__main__":
    main()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值