zoukankan      html  css  js  c++  java
  • 第一讲 基本概念

    01-复杂度1:最大子列和问题
    Description:

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

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

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

    Input:

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

    Output:

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

    SampleInput:

    6
    -2 11 -4 13 -5 -2

    SampleOutput:

    20

    Codes:
    //#define LOCAL
    
    #include <cstdio>
    
    #define M 100010
    int A[M];
    
    int maxV(int a, int b, int c) {
    	int t = a>b?a:b;
    	return t = t>c?t:c;
    }
    
    int mSub(int l, int r) {
    	if(l == r) return A[l];
    	int a, b, c, d, i, u, v, m = (l+r)/2;
    	a = b = c = d = 0, u = mSub(l, m), v = mSub(m+1, r);
    	for(i=m; i>=l; --i) {
    		a += A[i];
    		if(a > c) c = a;
    	}
    	for(i=m+1; i<=r; ++i) {
    		b += A[i];
    		if(b > d) d = b;
    	}
    	return maxV(u, v, c+d);
    }
    
    int main()
    {
    	#ifdef LOCAL
    		freopen("E:\Temp\input.txt", "r", stdin);
    		freopen("E:\Temp\output.txt", "w", stdout);
    	#endif
    
    	int i, n, sum = 0;
    	scanf("%d", &n);
    	for(i=0; i<n; ++i) {
    		scanf("%d", &A[i]);
    		if(A[i] < 0) ++sum;
    	}
    
    	if(sum == n) printf("0
    ");
    	else printf("%d
    ", mSub(0, n-1));
    
    	return 0;
    }
    
    PAT-1007:Maximum Subsequence Sum.
    Description:

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum 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:

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

    Output:

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

    SampleInput:

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

    SampleOutput:

    10 1 4

    Codes:
    //#define LOCAL
    
    #include <cstdio>
    
    #define M 10010
    int s, p, q, A[M];
    
    void mSub(int n) {
        int a = 0, b = 0, i;
        for(i=0; i<n; ++i) 
            if(A[i] >= 0) { p = q = i; break; }
        for(i=0; i<n; ++i) {
            a += A[i];
            if(a > s) { s = a; q = i; p = b; }
            if(a < 0) { a = 0; b = i+1; }
        }
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("E:\Temp\input.txt", "r", stdin);
            freopen("E:\Temp\output.txt", "w", stdout);
        #endif
    
        int i, n, f = 1;
        scanf("%d", &n);
        for(i=0; i<n; ++i) {
            scanf("%d", &A[i]);
            if(A[i] >= 0) f = 0;
        }
    
        mSub(n);
        if(f) { s = 0; p = 0; q = n-1; }
        printf("%d %d %d
    ", s, A[p], A[q]);
    
        return 0;
    }
  • 相关阅读:
    2020寒假 学习进度笔记4:spark使用1
    2020寒假 学习进度笔记3:Spark安装
    2020寒假 学习进度笔记2:Scala学习
    2020寒假 学习进度笔记1:Scala安装(Windows + Lunux)
    测试试卷—数据清洗
    实验6:Mapreduce实例——WordCount
    个人课程总结
    如何打jar包
    初探性能优化——2个月到4小时的性能提升(copy)推荐阅读
    《阿里巴巴Java工作手册》学习笔记
  • 原文地址:https://www.cnblogs.com/VincentValentine/p/6819153.html
Copyright © 2011-2022 走看看