[浙江大学数据结构]PAT算法题目解析

01-复杂度1 最大子列和问题(20 分)

给定K个整数组成的序列{ N1N2, ..., NK },“连续子列”被定义为{ NiNi+1, ..., Nj },其中 1ijK。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13}有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。 

本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:

  • 数据1:与样例等价,测试基本正确性;
  • 数据2:102个随机整数;
  • 数据3:103个随机整数;
  • 数据4:104个随机整数;
  • 数据5:105个随机整数;

输入格式:

输入第1行给出正整数K (100000);第2行给出K个整数,其间以空格分隔。 

输出格式:

在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0

 

时间复杂度为n的3次方的算法(暴力破解):

#include<iostream>

using namespacestd;

int main(){

  int n;

  cin>>n;

  int a[n];

  for(int i=0;i<n;i++){

    cin>>a[i];

  }

  int sum=0;

  int tsum;

  for(int i=0;i<n;i++){

    for(int j=i;j<n;j++){

      tsum=0;

      for(int k=i;k<=j;k++){

        tsum+=a[k];

      }

      if(tsum>sum)

         sum=tsum;

    }

  }

  cout<<sum<<endl;

  return 0;

}

显然上面的算法会超时!

时间复杂度为n的平方的算法:

#include<iostream>

using namespacestd;

int main(){

  int n;

  cin>>n;

  int a[n];

  for(int i=0;i<n;i++){

    cin>>a[i];

  }

  int sum=0;

  int tsum;

  for(int i=0;i<n;i++){

    tsum=0;

    for(int j=i;j<n;j++){

      tsum+=a[j];

      if(tsum>sum)

         sum=tsum;

    }

  }

  cout<<sum<<endl;

  return 0;

}

时间复杂度为N的算法:

#include<iostream>

using namespacestd;

int main(){

  int n;

  cin>>n;

  int a[n];

  for(int i=0;i<n;i++){

    cin>>a[i];

  }

  int sum=0;

  int tsum;

  for(int i=0;i<n;i++){

    tsum+=a[i];

    if(tsum>sum)

       sum=tsum;

    elseif(tsum<0)

       tsum=0;

  }

  cout<<sum<<endl;

  return 0;

}

(在线处理算法,边遍历,边处理)

时间复杂度为n的DP算法:

#include<iostream>

using namespace std;

int main(){

  int n;

  cin>>n;

  int a[n];

  for(int i=0;i<n;i++){

    cin>>a[i];

  }

  int b[n];

  b[0]=a[0];

  int sum=b[0];

  for(int i=0;i<n;i++){

    if(a[i]>(b[i-1]+a[i])){

           b[i]=a[i];

        }else{

           b[i]=b[i-1]+a[i];

        }

        sum=(sum>b[i])?sum:b[i];

  }

  cout<<sum<<endl;

  return 0;

}

 

复杂度2 MaximumSubsequence Sum(25 分)

Given a sequence of K integers { N1N2,..., NK }.A continuous subsequence is defined to be { NiNi+1,..., Nj }where 1ijK.The Maximum Subsequence is the continuous subsequence which has the largest sumof its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, itsmaximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum,together with the first and the last numbers of the maximum subsequence. 

Input Specification:

Each input file contains one test case. Each case occupies twolines. The first line contains a positive integer K (10000).The second line contains K numbers,separated by a space. 

Output Specification:

For each test case, output in one line the largest sum, togetherwith the first and the last numbers of the maximum subsequence. The numbersmust be separated by one space, but there must be no extra space at the end ofa line. In case that the maximum subsequence is not unique, output the one withthe smallest indices i and j (as shown bythe sample case). If all the K numbers arenegative, then its maximum sum is defined to be 0, and you are supposed tooutput the first and the last numbers of the whole sequence. 

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

 

 

#include <stdio.h>

 

#define  MAX100000ul

 

intMaxSubsequenceSum(int*p, int N, int*start, int*end)

{

    int i =0,count = 0;

    inttmp_start = 0;

    intThisSum = 0,MaxSum = 0;

    for (i= 0; i< N; i++)

    {

        ThisSum += *(p +i);

 

        if(ThisSum < 0)

        {

           count++;

            ThisSum= 0;

           tmp_start = i + 1;

        }

        elseif(ThisSum > MaxSum)

        {

            MaxSum= ThisSum;

            *start = *(p +tmp_start);

            *end = *(p +i);

        }

    }

    if(count == N)

    {

        MaxSum = 0;

        *start = *(p + 0);

        *end = *(p +N - 1);

    }

    returnMaxSum;

}

 

intmain(void)

{

    int i;

    long int k =0;

    intarr[MAX] = { 0 };

    intstart = 0,end = 0;

 

    scanf("%d",&k);

 

    for (i= 0; i< k; i++)

    {

        scanf("%d",&arr[i]);

    }

 

    printf("%d ",MaxSubsequenceSum(arr, k, &start, &end));

    printf("%d%d",start, end);

 

    return0;

}

发布了65 篇原创文章 · 获赞 101 · 访问量 40万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览