Algorithms: Design and Analysis, Part 1 【program 1/逆序数】

#include<string>
#include <vector>
#include <fstream>

using namespace std;

std::vector<int> v;
int bigArr[100000];
int helpArr[100000];

_int64 MergeAndCount( int* arr, int left, int mid, int right )
{
    if( left >= right )
    {
        return 0;
    }

    for( int i = left; i <= right; i++ )
    {
        helpArr[i] = arr[i];
    }

    int head1 = left;
    int head2 = mid + 1;

    int num = right - left + 1;

    int k = left;
    _int64 rNum = 0;
    for( int i = 0; i < num; i++, k++ )
    {
        if( head1 > mid )
        {
            arr[k] = helpArr[head2++];
        }
        else if( head2 > right )
        {
            arr[k] = helpArr[head1++];
        }
        else if( helpArr[head1] > helpArr[head2] )
        {
            rNum += ( mid - head1 + 1 );

            arr[k] = helpArr[head2++];
        }
        else
        {
            arr[k] = helpArr[head1++];
        }
    }

    return rNum;
}

_int64 GetR( int* arr, int left, int right )
{
    if( ( right - left ) <= 0 )
    {
        return 0;
    }

    int mid = ( left + right ) / 2;

    _int64 leftNum = GetR( arr, left, mid );
    _int64 rightNum = GetR( arr, mid+1, right );
    _int64 extraNum = MergeAndCount( arr, left, mid, right );

    return leftNum + rightNum + extraNum;
}

void main()
{
    fstream infile( "IntegerArray.txt" );
    string tmp;
    while( getline( infile, tmp ) )
    {
        int num = atoi( tmp.c_str() );
        v.push_back( num );
    }

    for( int i = 0; i < 100000; i++ )
    {
        bigArr[i] = v.at(i);
    }

    _int64 rNum = GetR( bigArr, 0, 100000 - 1 );
}
时间: 2024-10-09 21:16:18

Algorithms: Design and Analysis, Part 1 【program 1/逆序数】的相关文章

【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of times Insertion Sort

Algorithms: Design and Analysis, Part 1 【program 2/统计快排比较次数】

#include<string> #include <vector> #include <fstream> using namespace std; std::vector<int> v; int bigArr[10000]; int helpArr[10000]; int partition( int* arr, int left, int right ); int QSort( int* arr, int left, int right ) { if(

Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming assignment you will implement one or more of the integer multiplication algorithms described in lecture. To get the most out of this assignment, your pro

[Stanford Algorithms: Design and Analysis, Part 2]

Specific topics in Part 2 include: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes), dynamic programming (knapsack, sequence alignment, optimal search trees, shortest paths), NP-completeness and what it means for the

Algorithms: Design and Analysis Note

Week2 Master method Assumption:all subproblems has same size Recurrence Format The Master Method formula

[Stanford Algorithms: Design and Analysis, Part 2] c25 HUFFMAN CODES

原文地址:https://www.cnblogs.com/ecoflex/p/10577522.html

[Stanford Algorithms: Design and Analysis, Part 2] c27 The Knapsack Problem

原文地址:https://www.cnblogs.com/ecoflex/p/10640959.html

[Stanford Algorithms: Design and Analysis, Part 2] c28 Sequence Alignment Optimal Substructure

原文地址:https://www.cnblogs.com/ecoflex/p/10658398.html

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no