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 ai,
that is, the distance from it to the neighboring skins and the book cover can’t be less than ai 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 ai (1
≤ ai ≤ 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)

为什么要这样呢?

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

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

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

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

#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;
}

最新文章

  1. 完美 全兼容 解决 文字两端对齐 justify 中文姓名对齐
  2. C#枚举中的位运算权限分配浅谈
  3. WinForm------GridControl中通过判断单元格文字显示不同字体颜色或背景色
  4. 【原】iOS学习之卸载Openfire
  5. 【原】macbook不睡眠的排查与解决
  6. android SurfaceView绘制 重新学习--基础绘制
  7. webapp之路--百度手机前端经验(转)
  8. 2017/10/10 jar包错误
  9. Linux多线程编程详细解析----条件变量 pthread_cond_t
  10. 从CUMT校园导航出现的问题看CSS布局设计(一) CSS盒模型
  11. Vue Affix组件
  12. JS基础概念
  13. Nginx 中 FastCGI 配置示例
  14. H3C ER6300 + 两台 H3C S5120 组网举例
  15. php-fpm慢日志配置
  16. synchronized和volatile
  17. Cassandra--设置数据保留时间
  18. FreeMarker的空值运算符和逻辑运算符
  19. JMeter学习(二十八)内存溢出解决方法(转载)
  20. sparse linear regression with beta process priors

热门文章

  1. DNS RR代码和含义
  2. hbase伪分布安装配置
  3. python 3.x 写 shell sed 替换功能
  4. Struts2的struts.xml的标准配置文档
  5. IHttpHandler的学习(1)
  6. 关于linq使用建议
  7. WLAN RTT (IEEE 802.11mc)
  8. 用xstart远程连接linux图形用户界面时发生已经在使用的情况
  9. spring data jpa实体类映射配置
  10. Jeff Dean专访,有不少干货