C. Fragile Bridges

题目连接:

http://codeforces.com/contest/201/problem/C

Description

You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there.

The bonus level consists of n small platforms placed in a line and numbered from 1 to n from left to right and (n - 1) bridges connecting adjacent platforms. The bridges between the platforms are very fragile, and for each bridge the number of times one can pass this bridge from one of its ends to the other before it collapses forever is known in advance.

The player's actions are as follows. First, he selects one of the platforms to be the starting position for his hero. After that the player can freely move the hero across the platforms moving by the undestroyed bridges. As soon as the hero finds himself on a platform with no undestroyed bridge attached to it, the level is automatically ended. The number of points scored by the player at the end of the level is calculated as the number of transitions made by the hero between the platforms. Note that if the hero started moving by a certain bridge, he has to continue moving in the same direction until he is on a platform.

Find how many points you need to score to be sure that nobody will beat your record, and move to the next level with a quiet heart.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of platforms on the bonus level. The second line contains (n - 1) integers ai (1 ≤ ai ≤ 109, 1 ≤ i < n) — the number of transitions from one end to the other that the bridge between platforms i and i + 1 can bear.

Output

Print a single integer — the maximum number of points a player can get on the bonus level.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample Input

5

2 1 2 1

Sample Output

5

Hint

题意

有n个点,n-1座桥,每座桥最多通过a[i]次,每通过一次可以获得1分

然后问你怎么选择起点和路线,才能获得最多的分数

题解:

dp

我们想想可以发现,我们令l[i]表示i点向左边走,且最后回到i点最多能得多少分,r[i]表示i点向右走,且最后回到i点最多能得多少分

odd[i]表示,从1号桥开始,走到i号桥,最多能得多少分

显然,我们这道题要求的最大值,应该就是l[i]-odd[i]+r[j]+odd[j]这个东西,使得这个东西最大就好了

我们暴力枚举j,然后每次用set去拿到最大的l[i]-odd[i]就好了

应该叫dp吧,大概 O.O

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long l[maxn],r[maxn],odd[maxn];
int n;
long long a[maxn];
int get(int x)
{
if(x%2==1)return x-1;
return x;
}
set<long long>s;
int main()
{
scanf("%d",&n);
for(int i=0;i<n-1;i++)
scanf("%d",&a[i]);
for(int i=1;i<n-1;i++)
if(a[i-1]>1)
l[i]=l[i-1]+get(a[i-1]);
for(int i=n-2;i>=0;i--)
if(a[i]>1)
r[i]=r[i+1]+get(a[i]);
for(int i=0;i<n-1;i++)
{
odd[i]+=get(a[i]-1)+1;
if(i>0)odd[i]+=odd[i-1];
}
long long ans = l[0]+r[0];
long long tmp = ans;
s.insert(l[0]);
for(int i=1;i<n;i++)
{
s.insert(l[i]-odd[i-1]);
long long p=*--s.lower_bound(1LL*1e16);
ans=max(r[i]+odd[i-1]+p,ans);
}
cout<<ans<<endl;
}

Codeforces Round #127 (Div. 1) C. Fragile Bridges dp的更多相关文章

  1. Codeforces Round #127 (Div. 2)

    A. LLPS 长度最大10,暴力枚举即可. B. Brand New Easy Problem 枚举\(n\)的全排列,按题意求最小的\(x\),即逆序对个数. C. Clear Symmetry ...

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. Codeforces Round #127 (Div. 1) E. Thoroughly Bureaucratic Organization 二分 数学

    E. Thoroughly Bureaucratic Organization 题目连接: http://www.codeforces.com/contest/201/problem/E Descri ...

  4. Codeforces Round #127 (Div. 1) D. Brand New Problem 暴力dp

    D. Brand New Problem 题目连接: http://www.codeforces.com/contest/201/problem/D Description A widely know ...

  5. Codeforces Round #127 (Div. 1) B. Guess That Car! 扫描线

    B. Guess That Car! 题目连接: http://codeforces.com/contest/201/problem/B Description A widely known amon ...

  6. Codeforces Round #127 (Div. 1) A. Clear Symmetry 打表

    A. Clear Symmetry 题目连接: http://codeforces.com/contest/201/problem/A Description Consider some square ...

  7. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

  8. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

  9. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp

    B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...

随机推荐

  1. windows下常用快捷键(转)

    原文转自 https://blog.csdn.net/LJFPHP/article/details/78818696 win+E                 打开文件管器 win+D        ...

  2. selenium===介绍

    selenium 是支持java.python.ruby.php.C#.JavaScript . 从语言易学性来讲,首选ruby ,python 从语言应用广度来讲,首选java.C#.php. 从语 ...

  3. STL容器之间的差异和联系

     1.vector  (连续的空间存储,可以使用[]操作符)快速的访问随机的元素,快速的在末尾插入元素,但是在序列中间的插入,删除元素要慢(涉及元素复制移动),而且如果一开始分配的空间不够的话,有一个 ...

  4. [How to]Cloudera manager 离线安装手册

    2016-01-1910:54:05  增加kafka 1.简介 本文介绍在离线环境下安装Cloudera manager和简单使用方法 2.环境 OS:CentOS 6.7 Cloudera man ...

  5. JavaScript跨域解决方法大全

    跨域的定义:JavaScript出于安全性考虑,同源策略机制对跨域访问做了限制.域仅仅是通过“URL的首部”字符串进行识别,“URL的首部”指window.location.protocol +win ...

  6. Leetcode 之Count and Say(35)

    很有意思的一道题,不好想啊. string getNext(string &s) { ]; ; stringstream ss; ; i < s.size(); i++) { if (s ...

  7. SEO优化:WordPress站点地图(html和xml)插件Baidu Sitemap Generator

    前阵子分享了<如何实现纯代码制作网站地图的html和xml版本>,不过不是每个人都喜欢用纯代码来折腾博客的.今天,boke112就给大家分享一款国人柳城制作的包含html和xml两个版本的 ...

  8. 玩转树莓派 - 修改Raspbian软件源加快软件下载速度

    这是 meelo 原创的 玩转树莓派 系列文章 步骤1:登录到Raspbian的命令行界面 步骤2:修改Raspbian的软件源 软件源是Linux系统免费的应用程序安装仓库,很多的应用软件都会这收录 ...

  9. HBase shell 命令创建表及添加数据操作

    创建表,表名hbase_1102,HBase表是由Key-Value组成的,此表中Key为NAME   此表有两个列族,CF1和CF2,其中CF1和CF2下分别有两个列name和gender,Chin ...

  10. javascript大神修炼记(2)——运算符

    读者朋友们好,前面我已经大概的了解了Javascript的作用以及一些基本的函数声明与变量声明,今天我们就接着前面的内容讲解,我们就来看一下javscript的逻辑(正序,分支,循环)以及一些简单的运 ...