美文网首页
路上的球

路上的球

作者: loick | 来源:发表于2019-11-28 20:22 被阅读0次
Description

There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The buckets on both roads are kept in such a way that they are sorted according to the number of balls in them. Geek starts from the end of the road which has the bucket with a lower number of balls(i.e. if buckets are sorted in increasing order, then geek will start from the left side of the road). The geek can change the road only at the point of intersection(which means, buckets with the same number of balls on two roads). Now you need to help Geek to collect the maximum number of balls.

Input

The first line of input contains T denoting the number of test cases. The first line of each test case contains two integers N and M, denoting the number of buckets on road1 and road2 respectively. 2nd line of each test case contains N integers, number of balls in buckets on the first road. 3rd line of each test case contains M integers, number of balls in buckets on the second road.

Constraints:1<= T <= 1000,1<= N <= 10^3,1<= M <=10^3,0<= A[i],B[i]<=10^6

Output

For each test case output a single line containing the maximum possible balls that Geek can collect.

Sample Input
1
5 5
1    4 5 6 8
2 3 4    6 9
Sample Output
29

思路

把数组的位置调整一下看:

1 4 5 6 8
2 3 4 6 9

两条路在4的位置交叉,在6的位置交叉,两个数组被分为3段,能收集的最大值是三段中两条路的最大值。

max(1+4, 2+3+4) + max(5+6, 6) + max(8, 9) = 29

代码实现就简单了,

  1. 首先找出两个数组中共同有的数common
  2. common中的数把两个数组分割成若干个子数组
  3. 累加每段两个数组对应子数组中和最大的一个
python
def solve(balls1, balls2):
    common = set(balls1) & set(balls2)
    arr1, arr2 = [], []
    for num in sorted(common):
        idx1 = balls1.index(num)
        idx2 = balls2.index(num)
        arr1.append(sum(balls1[:idx1+1]))
        arr2.append(sum(balls2[:idx2+1]))
        balls1 = balls1[idx1+1:]
        balls2 = balls2[idx2+1:]
    ans = max(sum(balls1), sum(balls2))
    for val1, val2 in zip(arr1, arr2):
        ans += max(val1, val2)
    return ans
    
for _ in range(int(input())):
    input()
    balls1 = list(map(int, input().split()))
    balls2 = list(map(int, input().split()))
    print(solve(balls1, balls2))

相关文章

  • 路上的球

    Description There are two parallel roads, each containing...

  • 一天不见甚是想念-20221110

    放了学没看到球球,心里那个急啊,带着去商场的路上都哼唧着自己很久没和球球一起玩了,甚至一度决定东西都不买要去球球家...

  • 赞球羊胎素嫩肤原液,给你皮肤最好的呵护

    文/赞球科技美护 “保养皮肤是我们的终身事业”在追寻美的道路上,赞球始终走在前沿! 「赞球羊胎素嫩肤原液」,以多合...

  • 我们的挑战是如何实现自己的目的?

    球爸出差,我送球球去上幼儿园,平时球爸送他来回也就十多分钟,可我今天花了一个小时。 在路上我们还有说有笑,他骑车我...

  • 煎茶日记-球球回家

    今天中午和丹和她爸妈吃了北京烤鸭,吃了很多,然后下午去接球球回家,球球有点没精神,蔫了吧唧的,估计没好,回家路上发...

  • 周末时光

    周六 小家伙上午练习跆拳道 下午我们打羽毛球 我们在楼下打羽毛球 羽毛球地点换了两个地方 我们在马路上打羽毛球 结...

  • 今天没画球😁国庆在路上

    哈哈,今天国庆长假第二天,一整天都在路上。 昨天2点睡,6点起,果真放假比上班还累…… 今天的创作.欢迎拍砖…… ...

  • 球球,我想你了(一)

    球球在路上很安静,两只爪子扒在车窗下沿,脑袋半露出窗外,眼睛平视着前方,不知道在想些什么。一路上几乎都在保持着这个...

  • 国足的悲情

    奔跑,不停奔跑 奔跑在悲情路上 球轰然砸进网兜 够不到球的球鞋 无力抵抗的断肠 比分赫然定格眼前 踢不出球的步伐 ...

  • 说走就走的旅行,我也很羡慕啊

    “今晚就走。” 等我忙完反应过来,球球已经踏上了去扬州的路上。 我噼里啪啦的打字过去,“什么嘛?不是说好下周扬州见...

网友评论

      本文标题:路上的球

      本文链接:https://www.haomeiwen.com/subject/mkriwctx.html