NYOJ 745 蚂蚁的难题(二)

59 篇文章 0 订阅

OJ题目 : http://acm.nyist.net/JudgeOnline/problem.php?pid=745

描述

下雨了,下雨了,蚂蚁搬家了。

已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈。小蚂蚁对每种食材都有一个喜爱程度值Vi,当然,如果Vi小于0的时候,表示蚂蚁讨厌这种食材。因为马上就要下雨了,所以蚂蚁只能搬一次,但是能够搬走连续一段的食材。时间紧急,你快帮帮小蚂蚁吧,让它搬走的食材喜爱值和最大。

输入
有多组测试数据(以EOF结尾)。
每组数据有两行,第一行有一个n,表示有n种食材排成了一个圈。(2 <= n<= 50000)
第二行分别有n个数,代表蚂蚁对第n种食材的喜爱值Vi。(-10^9 <= Vi <= 10^9)
输出
输出小蚂蚁能够搬走的食材的喜爱值总和的最大。
样例输入
3
3 -1 2
5
-8 5 -1 3 -9
样例输出
5
7
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define SWAP(a , b){ int temp = a; a = b; b = temp;}

using namespace std;
typedef long long LL;

int x[50002];
int main()
{
    int n;
    while(cin >> n)
    {
        LL sum = 0;
        memset(x , 0 ,sizeof(x));
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&x[i]);
            sum += x[i];
        }
        LL nend = 0;
        LL maxsum = 0;
        for(int i = 1;i <= n;i++)
        {
            if(nend < 0)
                nend = 0;
            nend += x[i];
            maxsum = Max(nend , maxsum);
        }
        LL minsum = 0;
        for(int i = 1;i <= n;i++)
        {
            if(nend > 0)
                nend = 0;
            nend += x[i];
            minsum = Min(nend , minsum);
        }
        LL ans = Max(maxsum ,sum - minsum);
        cout << ans << endl;
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值