ms-onlinetest-question3

Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

Description

Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original inversion count.

Definition of Inversion: Let (A[0], A[1] ... A[n]) be a sequence of n numbers. If i < j and A[i] > A[j], then the pair (i, j) is called inversion of A.

Example:
Count(Inversion({3, 1, 2})) = Count({3, 1}, {3, 2}) = 2
InversionCountOfSwap({3, 1, 2})=>
{
  InversionCount({1, 3, 2}) = 1 <-- swapping 1 with 3, decreases inversion count by 1
  InversionCount({2, 1, 3}) = 1 <-- swapping 2 with 3, decreases inversion count by 1
  InversionCount({3, 2, 1}) = 3 <-- swapping 1 with 2 , increases inversion count by 1
}


Input

Input consists of multiple cases, one case per line.Each case consists of a sequence of integers separated by comma.

Output

For each case, print exactly one line with the new inversion count or the original inversion count if it cannot be reduced.


Sample In

3,1,2
1,2,3,4,5

Sample Out

1
0

算法:不过是每一项的nBigger(前面比它值大的项数)的累加和
         交换的话,只需考虑 较大值 和 较小值 的交换,反之显然只会增加inversion count.

测试代码:

#pragma once

#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX_DIGITS 20

struct DATA_STRUCT
{
    DATA_STRUCT(): nBigger(0),value(0) 
    {}
    int nBigger;
    int value;
};
typedef vector<DATA_STRUCT>  DATA_ARRAY;


void SwapInt(int& a, int& b);
int SwapAndCalcInversion(DATA_ARRAY arrData, int pos1, int pos2);

void RunQuest03()
{
    DATA_ARRAY arData;
    cout<<"enter an array of integers separated by comma or space:"<<endl;
    string str;
    cin>>str;
    //extract real data
    int i = 0;
    while(i < str.length())
    {
        char szNum[MAX_DIGITS] = {0};
        while( i < str.length() && str[i] >= '0' && str[i]<='9')
        {
            char szCr[2] = {0};
            szCr[0] = str[i++];
            szCr[1] = '';
            strcat(szNum, szCr);
        }

        if(strlen(szNum)>0)
        {
            DATA_STRUCT data;
            data.value = atoi(szNum);
            arData.push_back(data);
            //update data struct
            int nPosLast = arData.size()-1;
            for(int j = 0; j < nPosLast; ++j)
            {
                if(arData[j].value > arData[nPosLast].value)
                    arData[nPosLast].nBigger += 1;
            }            
        }
        i++;
    }

    //get the original inversion count
    int nInversion = 0;
    for(int i = 0; i <arData.size(); ++i)
        nInversion += arData[i].nBigger;
    cout<<"Original Inversion Count:  "<<nInversion<<endl;

    //cout<<"only swap the biggest  and the smallest :"<<endl;  //没有确定的快速算法的话 不使用

    cout<<"trying all possible to find one pair of integers that could reduce the inversion count.."<<endl;
    for(int i = 0; i < arData.size()-1; ++i)
    {
        for(int j = i +1; j < arData.size(); ++j)
        {
            if(arData[i].value > arData[j].value)
            {
                cout<<"Former "<<"Later (nInversion)"<<endl;
                cout<<nInversion<<"  -->  ";
                nInversion = SwapAndCalcInversion(arData,i, j) < nInversion ? SwapAndCalcInversion(arData,i, j) : nInversion;
                cout<<nInversion<<"		Swap Value:"<<arData[i].value<<"  "<<arData[j].value<<endl;
            }
        }
    }
    cout<<"Final inversion count: "<<nInversion<<endl;
}

void SwapInt(int& a, int& b)
{
    a = a + b;
    b = a - b;
    a = a - b;
}

int SwapAndCalcInversion(DATA_ARRAY arrData, int pos1, int pos2)
{
    SwapInt(arrData[pos1].value, arrData[pos2].value);
    arrData[pos1].nBigger = 0;
    arrData[pos2].nBigger = 0;

    //udpate the nBigger in zone [pos1, pos2]
    int nPos1 = pos1;
    int nPos2 = pos2;
    if(pos1 > pos2)
    {
        nPos1 = pos2;
        nPos2 = pos1;
    }
    for(int i = nPos1; i <= nPos2; ++i)
    {
        for(int j = 0; j < i ; ++j)
        {
            if(arrData[j].value > arrData[i].value)
                arrData[i].nBigger += 1;
        }
    }
    //get the inversion count
    int nInversion = 0;
    for(int i = 0; i <arrData.size(); ++i)
        nInversion += arrData[i].nBigger;
    return nInversion;
}

可以使用的一组测试值:2,0,5,8,1,4,7,2

最终代码:(不确定是否AC)

#pragma once
/*
@算法:不过是每一项的nBigger(前面比它值大的项数)的累加和
               交换的话,只需考虑 较大值 和 较小值 的交换,反之显然只会增加inversion count.
@date:    04/14/2014
@author: worksdata@163.com
*/

#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX_DIGITS 20

struct DATA_STRUCT
{
    DATA_STRUCT(): nBigger(0),value(0) 
    {}
    int nBigger;
    int value;
};
typedef vector<DATA_STRUCT>  DATA_ARRAY;


void SwapInt(int& a, int& b);
int SwapAndCalcInversion(DATA_ARRAY arrData, int pos1, int pos2);
void ProcessOneCase(string str);

void RunQuest03()
{
    typedef vector<string> STR_ARRAY;
    STR_ARRAY arStr;
    string str;
    while(getline(cin, str) && str.size() != 0)
    {
        arStr.push_back(str);
    }
    
    //process every case
    for(STR_ARRAY::iterator iter = arStr.begin(); iter != arStr.end(); ++iter)
        ProcessOneCase(*iter);
}

void ProcessOneCase(string str)
{
    DATA_ARRAY arData;
    //extract real data
    int i = 0;
    while(i < str.length())
    {
        char szNum[MAX_DIGITS] = {0};
        while( i < str.length() && str[i] >= '0' && str[i]<='9')
        {
            char szCr[2] = {0};
            szCr[0] = str[i++];
            szCr[1] = '';
            strcat(szNum, szCr);
        }

        if(strlen(szNum)>0)
        {
            DATA_STRUCT data;
            data.value = atoi(szNum);
            arData.push_back(data);
            //update data struct
            int nPosLast = arData.size()-1;
            for(int j = 0; j < nPosLast; ++j)
            {
                if(arData[j].value > arData[nPosLast].value)
                    arData[nPosLast].nBigger += 1;
            }            
        }
        i++;
    }

    //get the original inversion count
    int nInversion = 0;
    for(int i = 0; i <arData.size(); ++i)
        nInversion += arData[i].nBigger;

    //cout<<"trying all possible to find one pair of integers that could reduce the inversion count.."<<endl;
    for(int i = 0; i < arData.size()-1; ++i)
    {
        for(int j = i +1; j < arData.size(); ++j)
        {
            if(arData[i].value > arData[j].value)
                nInversion = SwapAndCalcInversion(arData,i, j) < nInversion ? SwapAndCalcInversion(arData,i, j) : nInversion;
        }
    }
    cout<<nInversion<<endl;
}


void SwapInt(int& a, int& b)
{
    a = a + b;
    b = a - b;
    a = a - b;
}

int SwapAndCalcInversion(DATA_ARRAY arrData, int pos1, int pos2)
{
    SwapInt(arrData[pos1].value, arrData[pos2].value);
    arrData[pos1].nBigger = 0;
    arrData[pos2].nBigger = 0;

    //udpate the nBigger in zone [pos1, pos2]
    int nPos1 = pos1;
    int nPos2 = pos2;
    if(pos1 > pos2)
    {
        nPos1 = pos2;
        nPos2 = pos1;
    }
    for(int i = nPos1; i <= nPos2; ++i)
    {
        for(int j = 0; j < i ; ++j)
        {
            if(arrData[j].value > arrData[i].value)
                arrData[i].nBigger += 1;
        }
    }
    //get the inversion count
    int nInversion = 0;
    for(int i = 0; i <arrData.size(); ++i)
        nInversion += arrData[i].nBigger;
    return nInversion;
}
原文地址:https://www.cnblogs.com/tupx/p/3663451.html