zoukankan      html  css  js  c++  java
  • UVA507-- Jill Rides Again

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=448

    典型的最大子段和问题。

    #include<iostream>
    using namespace std;
    int main()
    {
        int testNumber;     //测试的数目
        cin>>testNumber;
    
        for(int index=1;index<=testNumber;index++)  //循环测试每组数据
        {
            int max=0,sum=0,tempNumber=0;
            int tempStart=0;
            int start=0,end=0;
    
            int number;
            cin>>number;
            for(int i=0;i<number-1;i++)
            {
                cin>>tempNumber;
                sum+=tempNumber;
                if(sum<0)  //小于0的话暂时放弃之前的值,将下一个值作为起点
                {
                    tempStart=i+1;
                    sum=0;
                }
                if(sum>max||(sum==max&&start==tempStart))//只有在大于最大值的时候才会更新
                {
                    //一定要注意第二种情况,
                    //If more than one segment is maximally nice,
                    // choose the one with the longest cycle ride (largest j-i).
                    //To break ties in longest maximal segments,
                    //choose the segment that begins with the earliest stop (lowest i)
                    max=sum;
                    start=tempStart;
                    end=i;
                }
            }
            if(max==0)
               cout<<"Route "<<index<<" has no nice parts"<<endl;
            else
                cout<<"The nicest part of route "<<index<<" is between stops "<<start+1<<" and "<<end+2<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    数据加载存储和文件格式
    基本概率分布图的绘制
    pandas处理各类表格数据
    python时间处理
    matplotlib实现数据可视化
    sql学习笔记:表的运算
    sql杂记:一些坑和数据库恢复
    exists关键词和case表达式
    后台工具screen
    SQL函数小记
  • 原文地址:https://www.cnblogs.com/riskyer/p/3253580.html
Copyright © 2011-2022 走看看