Timus 1935. Tears of Drowned 详解

Old Captain Jack Sparrow’s friend Tia Dalma, the fortuneteller and prophetess, often makes potions. She has an outstanding collection of the rarest ingredients such as rat tails, fingers of drowned, tears of virgins and so on. And all these ingredients require special care.
Recently Tia Dalma received some good skins of bats as a payment, and now she wants to dry them. To dry ingredients fortuneteller usually uses specially prepared books as the magical properties of the skins could be lost during prolonged contact with other objects. Tia Dalma knows how many sheets should be on both sides of the skin to save it unspoiled. For a  i-th skin that number is  a i, that is, the distance from it to the neighboring skins and the book cover can’t be less than  a i sheets. Help a fortuneteller determine the minimum number of sheets that should be in the book to save rare ingredients from damage.

Input

The first line contains integer  n  that is the number of skins (1 ≤  n ≤ 100). The second line contains  n integers  a i (1 ≤  a i ≤ 100).

Output

Output the minimal required number of sheets in the book.

Sample

input output
3
5 10 3
28


这是个十分难理解的题目,一难:难理解题意; 二难:难理解算法

题意简略为:

把一些奇怪的湿蝙蝠皮夹在书中,每张蝙蝠皮都带一个值,每张蝙蝠皮的两边的书的页数不能少于这个值,求最小需要使用多少页书的书本?

很奇怪吧,不过她是说个魔法故事的,多奇怪都不奇怪,O(∩_∩)O~

本题算法:

例子中为什么是28呢?

误解:5页+max(5,10) + max(10, 3) + 3 = 28

正解:3页+max(3, 5)+max(5, 10)+10 = 28

再看一个例子:

6
1 3 2 5 4 6

误解:1页+max(1, 3) + max(3, 2)+max(2,5)+max(5,4)+max(4,6) + 6 = 29

正解:1页+max(1, 2) +max(2, 3) + max(3, 4)+max(4,5)+max(5,6)+6 = 27

这就可以看出规律来了:

先排序然后求解。

不过这个虽然是正确解,但是最佳是:sum+max(array)


为什么要这样呢?

看清楚题意,题目没有规定要使用上面顺序来放这些蝙蝠皮,所以我们可以随便安排这些蝙蝠皮的位置--要读题读出这个意思不容易啊。


那么为什么要由小到大安排呢?

因为我们必须要保证小的数值的蝙蝠皮必须要使用到,不能保证使用两次,那就保证使用一次 -- 那么就只能是由小到大安排了。


理解了意思之后,程序却是十分简单的:

#include <algorithm>
#include <iostream>
using namespace std;

void TearsofDrowned1935()
{
	int n = 0, a = 0, ans = 0, maxNum = 0;
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a;
		ans += a;
		maxNum = max(maxNum, a);
	}
	ans += maxNum;
	cout<<ans;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值