PTA Data Structures Problem Set Week 1 - Maximum Subsequence Sum Algorithm & Binary Search

发表于 2020-08-08 19:12 625 字 4 min read

cos avatar

cos

FE / ACG / 手工 / 深色模式强迫症 / INFP / 兴趣广泛养两只猫的老宅女 / remote

剑指offer day31 数学(困难)剑指offer day30 分治算法(困难)剑指offer day29 动态规划(困难)剑指offer day28 搜索与回溯算法(困难)剑指offer day27 栈与队列(困难)剑指offer day26 字符串(中等)剑指offer day25 模拟(中等)剑指offer day24 数学(中等)剑指offer day23 数学(简单)剑指offer day22 位运算(中等)剑指offer day21 位运算(简单)剑指offer day20 分治算法(中等)剑指offer day19 搜索与回溯算法(中等)剑指offer day18 搜索与回溯算法(中等)剑指offer day17 排序(中等)剑指offer day16 排序(简单)剑指offer day15 搜索与回溯算法(中等)剑指offer day14 搜索与回溯算法(中等)剑指offer day13 双指针(简单)剑指offer day12 双指针(简单)剑指offer day11 双指针(简单)剑指offer day10 动态规划(中等)剑指offer day9 动态规划(中等)剑指offer day8 动态规划(简单)剑指offer day7 搜索与回溯算法(简单)剑指offer day6 搜索与回溯算法(简单)剑指offer day5 查找算法(中等)剑指offer day4 查找算法(简单)剑指offer day3 字符串(简单)剑指offer day2 链表(简单)剑指offer day1 栈与队列(简单)冲刺春招-精选笔面试66题大通关day22冲刺春招-精选笔面试66题大通关day21冲刺春招-精选笔面试66题大通关day20冲刺春招-精选笔面试66题大通关day19冲刺春招-精选笔面试66题大通关18冲刺春招-精选笔面试66题大通关17冲刺春招-精选笔面试66题大通关16冲刺春招-精选笔面试66题大通关day15冲刺春招-精选笔面试66题大通关day14冲刺春招-精选笔面试66题大通关day13冲刺春招-精选笔面试66题大通关day12冲刺春招-精选笔面试66题大通关day11冲刺春招-精选笔面试66题大通关day10冲刺春招-精选笔面试66题大通关day9冲刺春招-精选笔面试66题大通关day8冲刺春招-精选笔面试66题大通关day7冲刺春招-精选笔面试66题大通关day6冲刺春招-精选笔面试66题大通关day5冲刺春招-精选笔面试66题大通关day4冲刺春招-精选笔面试66题大通关day3冲刺春招-精选笔面试66题大通关day2冲刺春招-精选笔面试66题大通关day12021 Fall PAT Grade B Real Exam Solutions and Post-Contest SummaryPAT Grade B Practice Reflections and Pitfall SummaryPTA Data Structures Problem Set Week 3 - Planting Trees (Binary Trees, etc.)PTA Data Structure Problem Set Week 11 -- Hash SearchPTA Data Structure Problem Set Week 10 -- Sorting (Part 2)PTA Data Structure Problem Set Week 9 -- Sorting (Part 1)PTA Data Structure Problem Set Week 8 -- Graphs (Part 3)PTA Data Structure Problem Set Week 7 -- Graphs (Part 2)PTA Data Structure Problem Set Week 6 -- Graphs (Part 1)PTA Data Structures Problem Set Week 5 - Heaps & Huffman Trees & Union-FindPTA Data Structures Problem Set Week 4 - Binary Search Trees & AVL TreesPTA Data Structures Problem Set Week 2 - Linear StructuresPTA Data Structures Problem Set Week 1 - Maximum Subsequence Sum Algorithm & Binary SearchMOOC ZJU Data Structures After-Class Problems Record - PTA Data Structures Problem Set (Complete)2020 Lanqiao Cup Provincial Mock Contest Problem Record
本文档是关于“复杂度”主题的题目集,包含三个问题:最大子列和问题(基本要求)、最大子序列和问题(考研真题,难度较高)以及二分查找(函数填空题)。每个题目均涉及算法设计与实现,重点考察动态规划、分治思想及边界处理,其中部分测试点易错,需特别注意。

This article has been machine-translated from Chinese. The translation may contain inaccuracies or awkward phrasing. If in doubt, please refer to the original Chinese version.

Problem Set Master Index

01-Complexity 1 Maximum Subsequence Sum Problem (20 pts)

Problem Link

This is the experiment problem for the 4 algorithms covered at the end of this lesson. It is a basic requirement and must be done;

Code

#include <iostream>
using namespace std;
int n, x, nowsum, maxsum;
int main() {
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> x;
        nowsum += x;
        if (nowsum > maxsum) {
            maxsum = nowsum;
        } else if(nowsum <= 0) {
            nowsum = 0;
        }
    }
    cout << maxsum << endl;
    return 0;
}

Test Cases

Insert image description here

01-Complexity 2 Maximum Subsequence Sum (25 pts)

Problem Link

This is a 2004 Zhejiang University Computer Science graduate re-examination problem. Requirements are slightly higher, optional.;

Problem Description

The problem asks to find the maximum subsequence sum, as well as the first and last numbers of the maximum subsequence. Note that if there are multiple maximum subsequences, take the one with the smallest index.

Code

#include <iostream>
using namespace std;
#define N 10002
int n, x, nowsum, maxsum, l, r, first,last;
int a[N];
int main() {
    bool allminus = true;
    cin >> n;
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    maxsum = -1;
    for(int i = 0; i < n; ++i) {
        if (a[i] >= 0)
            allminus = false;
        nowsum += a[i];
        if (nowsum > maxsum && nowsum >= 0) {//最大,更新maxsum和r
            maxsum = nowsum;
            r = i;
            first = l;
            last = r;
        } else if(nowsum < 0) {
            nowsum = 0;
            l = r = i+1;
        }
    }
    if (allminus) {
        cout << 0 << " " << a[0] << " " << a[n-1] << endl;
    } else cout << maxsum << " " << a[first] << " " << a[last] << endl;
    return 0;
}

Test Cases

Test cases are as follows; cases 5 and 6 are error-prone

Insert image description here

01-Complexity 3 Binary Search (20 pts)

Problem Link

This is a fill-in-the-function problem paired with the after-class discussion questions. If you have spare capacity and know C programming, you can give it a try. You only need to submit a function, not the main function or other functions. If you don’t know C, study the after-class discussion questions on binary search instead.

Code

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    scanf("%d", &X);
    P = BinarySearch( L, X );
    printf("%d\n", P);

    return 0;
}

/* 你的代码将被嵌在这里 */
Position BinarySearch( List L, ElementType X ) {
    if (L->Last == 0) return NotFound;
    int l, r, mid, ans;
    l = 1, r = L->Last;
    while(l <= r) {
        mid = (l + r) / 2;
        if(L->Data[mid] == X) {
            ans = mid;
            return ans;
        } else if(L->Data[mid] < X) {
            l = mid + 1;
        } else r = mid - 1;
    }
    return NotFound;
}

Test Cases

Test cases are as follows; case 5 is error-prone

Insert image description here

喜欢的话,留下你的评论吧~

© 2020 - 2026 cos @cosine
Powered by theme astro-koharu · Inspired by Shoka