uva-507

  题意:连续序列和最大,直接枚举。。。。。

代码跑了2.4s.QAQ

 
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::queue;
    using std::bitset;



    constexpr int N = 20001;
    int seg[N] = { 0 };
    void solve()
    {
        int n;
        cin >> n;
        int t;
        int cases = 1;
        while (n--) 
        {
            cin >> t;
            --t;
            for (int i=0;i<t;i++) 
                cin >> seg[i];
            int as=-1, ae=-1;
            int amax = -1;
            for (int s=0;s<t;s++) 
            {
                int curMax = 0;
                for (int e=s;e < t;e++) 
                {
                    curMax += seg[e];
                    if (curMax > amax)
                    {
                        amax = curMax;
                        as = s+1;
                        ae = e+2;
                    }
                    else if (curMax == amax&& as!=-1)
                    {
                        if (ae - as-1 < e - s)
                        {
                            as = s+1;
                            ae = e+2;
                        }
                    }
                }
                

            }
            if (as==-1) 
            {
                
                cout << "Route "<<cases<<" has no nice parts" << endl;
            }
            else
            {
                cout<<"The nicest part of route "<<cases <<" is between stops "<<as<<" and "<<ae<<endl;
            }
            cases++;
            

        }
          
    }
};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

 

 

下面这个代码只花了120ms.

再次叙述题意:对于一个连续序列,寻找连续和最大的开始下标和结束下标,如果有多最大和相等,寻找最长的,如果最长的一样,寻找开始小标最小的.

以下代码只用了一层循环。使用as和ae标识我们期望的结果下标.sum表示当前的和,s表示当期和开始的计算下标.

#include"pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::queue;
    using std::bitset;




    void solve()
    {
        int n;
        cin >> n;
        int t;
        int cases = 1;
        while (n--)
        {
            cin >> t;
            int as = 1, ae = 0;
            int s = 1;
            int ans = -1;
            int sum = 0;
            for (int i = 1;i < t;i++)
            {
                int k;
                cin >> k;
                sum += k;
                if (sum > ans || (sum == ans && (ae - as) < (i - s)))
                {
                    ans = sum;
                    as = s;
                    ae = i;
                }
                if (sum < 0)
                {
                    sum = 0;
                    s = i + 1;
                }
            }
            if (ans <= 0)
            {
                cout << "Route " << cases << " has no nice parts" << endl;
            }
            else
            {
                cout << "The nicest part of route " << cases
                    << " is between stops " << as << " and " << ae+1 << endl;
            }
            ++cases;
        }

    }
};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

 

posted on 2019-03-03 15:28 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10465854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值