zoukankan      html  css  js  c++  java
  • OUC_Summer Training_ DIV2_#12(DP1) 723

    这一次是做练习,主要了解了两个算法,最大子矩阵和,最长上升子序列。

    先看题好啦。

    A - To The Max
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

    As an example, the maximal sub-rectangle of the array:

    0 -2 -7 0
    9 2 -6 2
    -4 1 -4 1
    -1 8 0 -2

    is in the lower left corner:

    9 2
    -4 1
    -1 8

    and has a sum of 15.
     

    Input

    The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
     

    Output

    Output the sum of the maximal sub-rectangle.
     

    Sample Input

    4
    0 -2  -7  0
    9  2 -6  2
    -4  1 -4  1
    -1  8  0  -2 

    Sample Output

    15
     找最大子矩阵和,方法就是将矩阵按行压缩为一维矩阵,在该一维矩阵中找最大子段和,具体实施过程是(4 *4矩阵)第一行最大子段和记录,1 2 行压缩后找出最大子段和,与前面找到的比较留下较大的。1 2 3行  1 2 3 4行  第2行  2 3  行2 3 4 行。。。。。。依次遍历整个矩阵。
    #include<stdio.h>
    #include<string.h>    
    int a[101][101];
    int b[101];
    int f(int a[40],int n);
    int main()
    {
    
        int i,j,k,n,temp,ans ;
        while(scanf("%d",&n) != EOF)
        {
        for(i = 0;i < n;i++)
        {
            for(j = 0;j < n;j++)
            scanf("%d",&a[i][j]);
        }
        temp=ans=-0x7fffff;
        for(i = 0;i < n;i++)
        {
            memset(b,0,sizeof(b));
            for(j = i;j < n;j++)
            {
                for(k = 0;k < n;k++)
                {
                    b[k] += a[j][k];
                }
                temp = f(b,n);
                if(ans < temp) ans =temp;
            }
        }
        printf("%d
    ",ans);
        }
        
        return 0;
    }
    int f(int a[40],int n)
    {
        int max = a[0],ans,i,j;
        for(i = 0;i < n;i++)
        {
            if(max < a[i])max = a[i];
        }
        if(max < 0)return max;
        else
        {
            int  sum = 0;
            for(i = 0;i < n;i++)
            {
                sum += a[i];
                if(sum > max)max = sum;
                else if(sum < 0)sum = 0;
            }
        }
        return max;
    }
    View Code
    B - Stock Exchange
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for rising trends. Given a sequence of numbers p1, p2,...,pn representing stock prices, a rising trend is a subsequence pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. John’s problem is to find very quickly the longest rising trend.

    Input

    Each data set in the file stands for a particular set of stock prices. A data set starts with the length L (L ≤ 100000) of the sequence of numbers, followed by the numbers (a number fits a long integer). 
    White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

    Output

    The program prints the length of the longest rising trend. 
    For each set of data the program prints the result to the standard output from the beginning of a line.

    Sample Input

    6 
    5 2 1 4 5 3 
    3  
    1 1 1 
    4 
    4 3 2 1

    Sample Output

      3

      1 

    1

    Hint

    There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.
    这个题就是最长上升子序列,只输出长度就好了,做法是:从前向后遍历数组(字符串)。先将第一个数放入结果数组里,然后遇见一个数如果比结果数组中最大的数还要大就直接放在最大的数的后面,如果不是,从前向后遍历数组知道找到一个比该数大的数为止,用这个数替换结果数组中第一个比他大的数。这里用的是二分法。
    #include <iostream>
    #include<stdio.h>
    using namespace std;
    int a[150000],b[150000],t;
    void dog(int z)
    {
        int x,y,mid;
        x=1;
        y=t;
        while(x<=y)
        {
            mid=(x+y)/2;
            if (b[mid]==z) return ;//这里可以不return,令x = mid +1 即可
            else
                if (b[mid]>z) y=mid-1;
                else
                    x=mid+1;
        }
        if (x==t+1)//这里就是最大的直接放在后面
        {
            t++;
            b[t]=z;
        }
        else
            b[x]=z;
    }
    int main() {
        int n,i;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=n;i++)
                scanf("%d",&a[i]);
            t=1;
            b[1]=a[1];
            for(i=2;i<=n;i++)
            {
                dog(a[i]);
            }
            printf("%d
    ",t);
        }
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    PS后期合成,你和大神的差距就这5步!
    欧几里得算法:从证明等式gcd(m, n) = gcd(n, m mod n)对每一对正整数m, n都成立说开去
    谜题:过桥问题
    《世界是数字的》读书笔记第一部分_硬件篇
    完全偶图K(3,3)与完全图K5是否存在平面表示
    Dijkstar算法的数学原理
    LeetCode 141. Linked List Cycle
    LeetCode 155. Min Stack
    LeetCode 160. Intersection of Two Linked Lists
    LeetCode 165.Compare Version Numbers
  • 原文地址:https://www.cnblogs.com/lwy-kitty/p/3214006.html
Copyright © 2011-2022 走看看