两个算法题

题目描述:(minimum swaps)

Given a sequence, we have to find the minimum no of swaps required to sort the sequence.

分析:

formula:  no. of elements out of place  -  "cycles" in the sequence

A cycle is a set of elements, each of which is in the place of another.  So in example sequences { 2, 1, 4, 3}, there are two cycles: {1, 2} and {3, 4}.  1 is in the place where 2 needs to go, and 2 is in the place where 1 needs to go 1, so they are a cycle; likewise with 3 and 4.

 


The sequences {3, 2, 1, 5, 6, 8, 4, 7 }also has two cycles: 3 is in the place of 1 which is in the place of 3, so {1, 3} is a cycle; 2 is in its proper place; and 4 is in the place of 7 which is in the place of 8 in place of 6 in place of 5 in place of 4, so {4, 7, 8, 6, 5} is a cycle.  There are seven elements out of place in two cycles, so five swaps are needed.

实现:

e.g. { 2, 3, 1, 5, 6, 4}

231564 -> 6 mismatch 
two cycles -> 123 and 456 
swap 1,2 then 2,3, then 4,5 then 5,6 -> 4 swaps to sort 
 
Probably the easiest algorithm would be to use a bitarray. Initialize it to 0, then start at the first 0. Swap the number there to the right place and put a 1 there. Continue until the current place holds the right number. Then move on to the next 0 
 
Example: 
231564 
000000 
-> swap 2,3 
321564 
010000 
-> swap 3,1 
123564 
111000 
-> continue at next 0; swap 5,6 
123654 
111010 
-> swap 6,4 
123456 
111111 
-> bitarray is all 1's, so we're done. 

 

代码:

#include <iostream>
#include <algorithm>

using namespace std;


template <typename T>
int GetMinimumSwapsForSorted(T seq[], int n)
{
    bool* right_place_flag = new bool[n];
    T* sorted_seq = new T[n];
    int p ,q;
   
    copy(seq, seq + n, sorted_seq);
    sort(sorted_seq, sorted_seq + n);    可采用效率更高的排序算法
   
    for(int i = 0; i < n; i++)
    {
        if(seq[i] != sorted_seq[i])
            right_place_flag[i] = false;
        else
            right_place_flag[i] = true;
    }
   
    p = 0;
    int minimumswap = 0;
    while(1)
    {
        while(right_place_flag[p])
            p++;
        q = p + 1;
        此种找法只对无重复序列能得出minimum swaps
        while(q < n)
        {
            if(!right_place_flag[q] && sorted_seq[q] == seq[p])
                break;
            q++;
        }

        if(q >= n || p >= n)
            break;
        right_place_flag[q] = true;
        if(seq[q] == sorted_seq[p])
            right_place_flag[p] = true;
        swap(seq[p], seq[q]);
        minimumswap++;
    }

    delete[] sorted_seq;
    delete[] right_place_flag;

    return minimumswap;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int seq[] = {3, 2, 1, 5, 6, 8, 4, 7 };//{2,3,1,5,6,4};//{2,3,2,4,7,6,3,5};
    int n = sizeof(seq) / sizeof(int);
    cout<<"minimum swaps : "<<GetMinimumSwapsForSorted(seq, n)<<endl;

    system("pause");
 return 0;
}

题目描述:(删除序列中所有重复的元素)

 

Given an unsorted n element array[0....n-1] not containing any numbers outside the range 1 to n. We need to remove all duplicates from this array in constant space and O(n) time.

分析:

array[0...n-1]取值[1,n],可以转换思路对序列中存在的元素作标记,然后输出这些元素便去掉了序列中所有的重复元素,时间复杂度为O(n)

代码:

#include <iostream>
#include <math.h>

using namespace std;

void RemoveAllDuplicates(int seq[], int n)
{
    // if element e exists in the array, make index (e-1) negative 
    for(int i = 0; i < n; i++) 
        seq[abs(seq[i]) - 1] = -abs(seq[abs(seq[i]) - 1]);

    // if index (e-1) is negative, print element e 
    cout<<endl;
    for(int i = 0; i < n; i++) 
        if(seq[i] < 0) 
            cout<<i + 1<<"/t"; 
    cout<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int seq[] = { 5,3,4,2,3,2,2,5};
    RemoveAllDuplicates(seq, 8);

    system("pause");
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值