码迷,mamicode.com
首页 > 其他好文 > 详细

Kick Start 2018-Round H-Problem B. Mural

时间:2018-12-01 23:42:37      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:like   erp   观察   sub   count   case   eve   价值   长度   

Problem

Thanh wants to paint a wonderful mural on a wall that is N sections long. Each section of the wall has a beauty score, which indicates how beautiful it will look if it is painted. Unfortunately, the wall is starting to crumble due to a recent flood, so he will need to work fast!

At the beginning of each day, Thanh will paint one of the sections of the wall. On the first day, he is free to paint any section he likes. On each subsequent day, he must paint a new section that is next to a section he has already painted, since he does not want to split up the mural.

At the end of each day, one section of the wall will be destroyed. It is always a section of wall that is adjacent to only one other section and is unpainted (Thanh is using a waterproof paint, so painted sections can‘t be destroyed).

The total beauty of Thanh‘s mural will be equal to the sum of the beauty scores of the sections he has painted. Thanh would like to guarantee that, no matter how the wall is destroyed, he can still achieve a total beauty of at least B. What‘s the maximum value of B for which he can make this guarantee?
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing an integer N. Then, another line follows containing a string of N digits from 0 to 9. The i-th digit represents the beauty score of the i-th section of the wall.
Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum beauty score that Thanh can guarantee that he can achieve, as described above.
Limits

1 ≤ T ≤ 100.
Small dataset

2 ≤ N ≤ 100.
Large dataset

For exactly 1 case, N = 5 × 106; for the other T - 1 cases, 2 ≤ N ≤ 100.
Sample

Input
4
4
1332
4
9583
3
616
10
1029384756

output:
Case #1: 6
Case #2: 14
Case #3: 7
Case #4: 31

In the first sample case, Thanh can get a total beauty of 6, no matter how the wall is destroyed. On the first day, he can paint either section of wall with beauty score 3. At the end of the day, either the 1st section or the 4th section will be destroyed, but it does not matter which one. On the second day, he can paint the other section with beauty score 3.

In the second sample case, Thanh can get a total beauty of 14, by painting the leftmost section of wall (with beauty score 9). The only section of wall that can be destroyed is the rightmost one, since the leftmost one is painted. On the second day, he can paint the second leftmost section with beauty score 5. Then the last unpainted section of wall on the right is destroyed. Note that on the second day, Thanh cannot choose to paint the third section of wall (with beauty score 8), since it is not adjacent to any other painted sections.

In the third sample case, Thanh can get a total beauty of 7. He begins by painting the section in the middle (with beauty score 1). Whichever section is destroyed at the end of the day, he can paint the remaining wall at the start of the second day.

t = int(input())
for case in range(t):
    n = int(input())
    s = input()
    l = []
    for i in range(len(s)):
        l.append(int(s[i]))
    # print(l)
    l1 = (n+1)//2
    count = sum(l[:l1])
    temp = count
    for i in range(l1,len(l)):
        temp = temp + l[i] - l[i-l1]
        count = max(count,temp)
    print("Case #",end=‘‘)
    print(case+1,end=‘‘)
    print(": ",end=‘‘)
    print(count)

讲道理,这道题很简单。代码也是三道题里最短的。读懂题目有些难度,大意是有一睹墙,画家从任意一个起点开始修复这面墙,获得这面墙的价值,然后洪水会从最左边或者最右边侵蚀一睹墙,已经被侵蚀的墙则不可以再被修复。然后画家向左或是向右继续修复然后洪水亦复如是。直到所有的墙都被修复或被侵蚀为止。求画家可以获得的最大价值。
如果用模拟法,一步步考虑地话就进坑了。观察到,画家可以修复的墙的长度是可以直接确定的(n+1)//2,而修复的墙总是连续的,所以也就是求(n+1)//2长度的最大值。
这里的一个优化就是,使用前缀和来减少计算量。
small和large都可以通过。

Kick Start 2018-Round H-Problem B. Mural

标签:like   erp   观察   sub   count   case   eve   价值   长度   

原文地址:https://www.cnblogs.com/bernieloveslife/p/10051207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!