• 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;
    }
  • 相关阅读:
    rocketmq的broker如何同步信息的?
    SO_LINGER 选项
    哈哈哈
    NAG博客目录
    事后分析$eta$
    项目展示$eta$
    测试报告$eta$
    发布声明$eta$
    Scrum meeting 10
    技术博客6--Django实现列表分页
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10465854.html
Copyright © 2020-2023  润新知