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

(环形DP) zoj 3310

时间:2015-04-16 23:44:52      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

W - Unrequited Love
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

Owen had been through unrequited love with N MM for a long time. In order to celebrate the 8th anniversary of ZOJ, he decided to "watch white".

One night, MMs are sitting in a ring. Owen‘s plan is to give them love letters secretly so that MMs who receive letters will know that Owen loves them. Besides, each MM has a "happy point". The greater it is, the happier Owen will be if that MM receives his love letter.

One more problem. MM is very sensitive, so she will know if Owen (even secretly) drops a letter to the girl next to her. Meanwhile, MM is likely to be jealous. If she receives the love letter and finds that some other girl also gets a love letter, she will pia Owen to death.

Owen, as a smart boy, won‘t let MM pia him, and decides to program to calculate the maximum sum of happy points he can get. However, love is blind. Poor Owen has lost the ability to program. So he turns to you.

Input

 

There are no more than 10 test cases. 
Each test case begins with an integer N (1 <= N <= 1000000). 
Then followed N integers(0<= Ni <= 1000), each representing a happy point for one MM. The first and last MM sit next to each other, forming a ring. 
Each test case occupies one line. 

Output

 

One line for each test case. 
An interger for each line indicating the maximum sun of happy points Owen can get. 

 

Sample Input

 

5 1 2 3 4 5
6 1 2 3 4 5 6

 

Sample Output

 

8
12
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
int dp1[1000005];
int dp2[1000005];
int a[1000005];
int n;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int ans;
        for(int i=1;i<=n;i++)
        {
            dp1[i]=0;
            dp2[i]=0;
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        dp1[1]=a[1];
        for(int i=2;i<=n-1;i++)
        {
            dp1[i]=dp1[i-2]+a[i];
            if(dp1[i]<dp1[i-1])
                dp1[i]=dp1[i-1];
        }
        for(int i=2;i<=n;i++)
        {
            dp2[i]=dp2[i-2]+a[i];
            if(dp2[i]<dp2[i-1])
                dp2[i]=dp2[i-1];
        }
        ans=max(dp1[n-1],dp2[n]);
        printf("%d\n",ans);
    }
    return 0;
}

  

(环形DP) zoj 3310

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4433593.html

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