Prob_Solve 板


LINE

※ 引述《Pttgambler ( )》之铭言: : 最近在 Leetcode 上面看到有人分享的一题线上测验, : 想了一段时间都没有想出暴力解以外的做法,所以来这里问看看。 : https://leetcode.com/discuss/interview-question/4902893/recent-goldman-sachs-oa-questionanybody-with-a-optimal-approach : In a tranquil village school, there are two students named Ramu and Sonu, : each possessing a collection of N distinct chalks. Each student's chalks are : of different lengths, represented by N positive integers. Ramu has arranged : his collection of N chalks in a specific order based on their lengths. Sonu : is eager to organize his own N chalks in a way that mimics Ramu's arrangement : in terms of length changes i.e. if in Ramu's arrangement the kth chalk is : bigger than the k+1th chalk, in Sonu's arrangement also the kth chalk will be : bigger than the k-1th chalk; alternately if it is smaller in Ramu's : arrangement, then it will be smaller in Sonu's as well.Sonu was busy : arranging his chalks, when his teacher told him to also maximize the overall : "niceness" of his arrangement. Here, niceness' is defined as the sum of the : absolute length differences between all adjacent chalks in the : arrangement.Write a program to assist Sonu in achieving both the objectives: : first, to mimic Ramu's length variation order, and second, to maximize the : overall niceness of the arrangement. : Sample Input1: : 4 : 5 7 4 9 : 1 2 3 4 : Sample Output1: : 7 : Explanation1: : Given N=4. : Ramu's chalks are arranged in the order of their length as 5 7 4 9, which : corresponds to an increase- decrease-increase pattern of arrangement. Sonu : has the chalk collection 12:34. : To mimic Ramu's chalk arrangement order of increase-decrease-increase, Sonu : can arrange his chalk in the following five ways. : (1,3,2,4)-niceness-> |1-3|+|3-2|+|2-4|=2+1+2=5 : (1,4,2,3)-niceness-> |1-4|+|4-2|+|2-3|=3+2+1=6 : (2,3,1,4)-niceness-> |2-3|+|3-1|+|1-4|=1+2+3=6 : (2,4,1,3)-niceness-> |2-4|+|4-1|+|1-3|=2+3+2=7 : (3,4,1,2)-niceness-> |3-4|+|4-1|+|1-2|=1+3+1-5 : As can be seen, the maximum niceness possible is 7, which is printed as : output. 令两个输入为数列 a[] 和 b[] 令最佳的 b[] 顺序为 B[] // sorted(b) == sorted(B) // answer == niceness(B) niceness(B) 是阵列中相邻两个数的差,对每个 B[i] ,有三个 case: 1. B[i] is a local maximum: 计算 niceness 时,会算到 (B[i] - B[i + 1]) + (B[i] - B[i - 1]) 只考虑 B[i] 的贡献的话,就是 2*B[i] 或是 B[i] (B[i-1], B[i+1] 可能不存在) 2. B[i] is a local minimum: 和前一个状况对称,B[i] 的贡献是 -2*B[i] 或是 -B[i] 3. 其他 - B[i-1], B[i+1] 一定都存在 - B[i-1] < B[i] < B[i+1] 或 B[i+1] < B[i] < B[i-1] 洽有一个成立 计算 niceness 时,会算到 (B[i] - B[i-1]) + (B[i+1] - B[i]) 或是 (B[i] - B[i+1]) + (B[i-1] - B[i]) ==> B[i] 对 niceness 没有贡献 我们先用 a 找到 local maximum 的位置 M[] 和 local minimum 的位置 m[] 假设我们已经找到排列 B ,则: def niceness(B: int[], M: int[], m: int[]): val = 0 for i in M: if i == 0 or i + 1 == len(B): val += B[i] else: val += 2*B[i] for i in m: if i == 0 or i + 1 == len(B): val -= B[i] else: val -= 2 * B[i] return val B[i] 只有五种可能的贡献方式: 1) 2 * B[i] 2) B[i] 3) 0 4) -B[i] 5) -2*B[i] 我们将 b 由大排到小,依序用 (1), (2), (3), (4), (5) 的方式贡献 这样算出来的 niceness 就会是最大值 def cmp(x, y): if x < y: return -1 if x > y: return 1 raise ValueError('There are duplicated values') def solve(n, a, b): c = [0] * n for i in range(n): if i > 0: c[i] += cmp(a[i], a[i-1]) if i + 1 < n: c[i] += cmp(a[i], a[i+1]) b = sorted(b) c = sorted(c) return sum(b[i] * c[i] for i in range(n)) 证明这样的 B 是存在的: 我们先把 b 照大小排列,依照 (1), (2), (3), (4), (5) 分组 (1) b[0] > b[1] > b[2] > b[3] > ... (2) b[k] // 也可能不存在,也可能有两个数 (3) b[k+1], b[k+2], ..., b[l-1] (4) b[l] // 也可能不存在,也可能有两个数 (5) b[l+1] > b[l+2] > ... > b[n-1] 这样一定可以满足:(1), (2) 的任意值 > (3) 的任意值 > (4),(5) 的任意值 b[k], b[l] 会被放在 b[0] 或 b[n-1] 的位置 而其他位置的极值可以分别从 (1) 和 (5) 任选 在 a[] 中,极大值和极小值一定是依序出现的, 极大值跟极小值中间可以间隔数个中间值, 我们可以从 (3) 里面依序挑出足够的数量,并用正确的顺序放入就可以了 用这种方式构造出来的 B 一定可以满足条件 (i) B[i], B[i+1] 的相对大小和 a[i], a[i+1] 一样 (ii) B 的 niceness 是最大值 我把讨论串里面的几组测资打进去答案都一样,希望没有漏掉什麽 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 104.133.122.86 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Prob_Solve/M.1711712753.A.A71.html
1F:推 Pttgambler: 哇!感觉就是这样了,怎麽想出来的啊 03/29 22:13
2F:→ Pttgambler: 谢谢 03/29 22:14
3F:→ stimim: 先观察到非极值的数字是没有影响的,再考虑极值的关系 04/01 10:42
4F:→ stimim: 一开始的猜想是如果在极值的部分照大小排列行不行 04/01 10:42
5F:→ stimim: 然後发现两边的端点需要特别处理 04/01 10:43







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP