Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N)O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elements, which in turn precede "true" elements. You may use only constant extra space.

Format of functions:

void MySort( ElementType A[], int N );

where ElementType A[] contains the N elements.

Sample program of judge:

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

typedef enum { true, false, maybe } Keys;
typedef Keys ElementType;

void Read( ElementType A[], int N ); /* details omitted */

void MySort( ElementType A[], int N );

void PrintA( ElementType A[], int N )
{
    int i, k;

    k = i = 0;
    for ( ; i<N && A[i]==false; i++ );
    if ( i > k )
        printf("false in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==maybe; i++ );
    if ( i > k )
        printf("maybe in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==true; i++ );
    if ( i > k )
        printf("true in A[%d]-A[%d]\n", k, i-1);
    if ( i < N )
        printf("Wrong Answer\n");
}

int main()
{
    int N;
    ElementType *A;

    scanf("%d", &N);
    A = (ElementType *)malloc(N * sizeof(ElementType));
    Read( A, N );
    MySort( A, N );
    PrintA( A, N );
    return 0;
}

/* Your function will be put here */

Sample Input:

6
2 2 0 1 0 0

Sample Output:

false in A[0]-A[0]
maybe in A[1]-A[2]
true in A[3]-A[5]
//
//  main.c
//  Sort Three Distinct Keys
//
//  Created by 余南龙 on 2016/12/9.
//  Copyright © 2016年 余南龙. All rights reserved.
//

void MySort( ElementType A[], int N ){
    int T[N], F[N], M[N];
    , j = , k = , index;

    ; index < N; index++){
        if(true == A[index]){
            i++;
        }
        else if(false == A[index]){
            j++;
        }
        else if(maybe == A[index]){
            k++;
        }
    }
    ; index < j; index++){
        A[index] = false;
    }
    for( ; index - j< k; index++){
        A[index] = maybe;
    }
    for( ; index - k - j < i; index++){
        A[index] = true;
    }
}

PTA Sort Three Distinct Keys的更多相关文章

  1. NSMutable sort排序

    Compare method Either you implement a compare-method for your object: - (NSComparisonResult)compare: ...

  2. .NET 排序 Array.Sort<T> 实现分析

    System.Array.Sort<T> 是.NET内置的排序方法, 灵活且高效, 大家都学过一些排序算法,比如冒泡排序,插入排序,堆排序等,不过你知道这个方法背后使用了什么排序算法吗? ...

  3. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  4. 使用DBMS_STATS来收集统计信息【转】

    overview Oracle's cost-based optimizer (COB) uses statistics to calculate the selectivity (the fract ...

  5. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  6. sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter

    依据Spark 1.4版 在哪里会用到它 ExternalSorter是Spark的sort形式的shuffle实现的关键.SortShuffleWriter使用它,把RDD分区中的数据写入文件. o ...

  7. How To Size Your Apache Flink® Cluster: A Back-of-the-Envelope Calculation

    January 11, 2018- Apache Flink Robert Metzger and Chris Ward A favorite session from Flink Forward B ...

  8. 算法(第四版)C# 习题题解——2.3

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更为方便的版本见:http ...

  9. 11G新特性 -- Multicolumn Statistics (Column groups)

    默认oracle会收集表中各个列的统计信息,但是会忽略列之间的关联关系.在大多情况下,优化器假设在复杂查询中的列之间是独立的.当where子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...

随机推荐

  1. C#预编译

    在C#开发中经常会遇到在Debug和Release模式下需要执行不同代码的情况,这个时候使用C#条件编译可以很好地解决这个问题,如 #if DEBUG // TO Do Debug Mode Code ...

  2. Struts2:上传下载

    ud_upload.jsp <s:form action="fileupload" enctype="multipart/form-data"> & ...

  3. SDE用户密码到期

    系统使用Arcsde for oracle 11g ,一直使用正常,突然有一天客户打电话说系统中地图不显示了.登陆GIS服务器,发现sde数据已经无法链接,错误提示:“ [ORA-28002: the ...

  4. git 源代码下载和安装(CentOS)

    1.阅读INSTALL文件 $ make configure ;# as yourself$ ./configure --prefix=/usr ;# as yourself$ make all do ...

  5. 解决sublime text3 文件名,小框框的办法

    解决sublime text3 文件名,小框框的办法 之前一直都是用的英文命名的文件夹,到前几天才发现,用中文,来命名文件夹出现了乱码问题. 今天晚上,自己也在网上去百度了很多方案,好像大部分都不太有 ...

  6. C/C++程序员应聘试题剖析(转载)

    转载自:http://www.cnitblog.com/zouzheng/articles/21856.html 1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面 ...

  7. 用php怎么改文件名

    php手册:bool rename ( string oldname, string newname [, resource context] )尝试把 oldname 重命名为 newname. 如 ...

  8. uva11292 Dragon of Loowater

    水题,排序遍历即可 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  9. oracle中字符串连接用||

    oracle中字符串连接用|| create or replace procedure testIf(idid number) is v_name stu.name%type; v_age stu.a ...

  10. Web APi之安装配置实现Cors跨域

    参考:http://www.cnblogs.com/CreateMyself/p/4836628.html 1.通过NuGet下载程序包,搜索程序包[Microsoft.AspNet.WebApi.C ...