博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HackerRank - Bricks Game
阅读量:4638 次
发布时间:2019-06-09

本文共 1590 字,大约阅读时间需要 5 分钟。

One sentence in the problem statement is crucial "your friend will also play optimally", if you interpret it correctly, you are very close to AC. What does it mean? It means, no matter what your current choice is, 1 brick or 2 or 3, your opponent will take optimized score of the rest bricks. Then, it is natural to have a 'reverse' DP:

(thanks to http://massivealgorithms.blogspot.com/2015/01/hackerrank-play-game.html)

t = int(input())for i in range(t):    n = int(input())    arr = [int(i) for i in input().strip().split()]        ######    def calc(arr):        alen = len(arr)        if alen < 4:            return sum(arr)        '''        Both DP[] and PreSum[] is in terms of bottom-up        because opponent 'your friend will also play optimally'        '''        #    Calc prefix sum        presum = [0] * alen        presum[0] = arr[0]        for i in range(1, alen):            presum[i] = presum[i - 1] + arr[i]                    #    Go DP        dp = [0] * (alen)                dp[0] = arr[0]        dp[1] = arr[1] + dp[0]        dp[2] = arr[2] + dp[1]        for i in range(3, alen):            # Take 1: (i), opponent will take dp[i - 1]            x = arr[i] + presum[i - 1] - dp[i - 1]            # Take 2            y = presum[i - 2] + arr[i] + arr[i - 1] - dp[i - 2]            # Take 3            z = presum[i - 3] + arr[i] + arr[i - 1] + arr[i - 2] - dp[i - 3]            dp[i] = max(x, y, z)                    return dp[alen - 1]    ######        # reverse to bottom -> top    arr.reverse()    print (calc(arr))

转载于:https://www.cnblogs.com/tonix/p/4364603.html

你可能感兴趣的文章
git命令详解
查看>>
C++函数声明后面加throw()的作用
查看>>
XA 事务
查看>>
C++ 模板元编程 学习笔记
查看>>
静态联编与动态联编
查看>>
虚函数本质
查看>>
异质链表
查看>>
linux 学习笔记二
查看>>
linux 学习笔记一
查看>>
linux 学习笔记四
查看>>
linux 学习笔记三
查看>>
Spring Boot浅谈(是什么/能干什么/优点和不足)
查看>>
关于JDK和eclipse的安装和汉化
查看>>
PostgreSQL-6-数据分组
查看>>
asyncio的简单了解
查看>>
2019暑假实习
查看>>
WebBrowser IE Version
查看>>
hdu 1992
查看>>
ADO.NET的ORACLE数据库操作
查看>>
The Havel-Hakimi Algorithm
查看>>