USACO——Sorting a Three-Valued Sequence

来源:互联网 发布:淘宝商城源码 编辑:程序博客网 时间:2024/06/10 15:47

Sorting a Three-Valued Sequence 
IOI'96 - Day 2

Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at mostthree different key values. This happens for instance when we sort medalists of a competition according to medal value, that is, gold medalists come first, followed by silver, and bronze medalists come last.

In this task the possible key values are the integers 1, 2 and 3. The required sorting order is non-decreasing. However, sorting has to be accomplished by a sequence of exchange operations. An exchange operation, defined by two position numbers p and q, exchanges the elements in positions p and q.

You are given a sequence of key values. Write a program that computes the minimal number of exchange operations that are necessary to make the sequence sorted.

PROGRAM NAME: sort3

INPUT FORMAT

Line 1:N (1 <= N <= 1000), the number of records to be sortedLines 2-N+1:A single integer from the set {1, 2, 3}

SAMPLE INPUT (file sort3.in)

9221333231

OUTPUT FORMAT

A single line containing the number of exchanges required

SAMPLE OUTPUT (file sort3.out)

4
思路:统计1、2 、3个数a,b,c则分别在(0,a)、(a,a+b)、(a+b、a+b+c)区间内,于是在各区间内的移出不合适的数字,移入合适的位置即可

/*ID: youqihe1PROG: sort3LANG: C++*/#include <iostream>#include <fstream>#include <string>#include <string.h>using namespace std;struct data{    int val;    int loc;}A[1005];int main() {    FILE *fin  = fopen ("sort3.in", "r");    FILE *fout = fopen ("sort3.out", "w");    int n,a=0,b=0,c=0;    fscanf(fin,"%d",&n);    int i,j,k;    for(i=0;i<n;i++)    {        fscanf(fin,"%d",&A[i].val);        A[i].loc=i;        switch(A[i].val)        {            case 1:a++;break;            case 2:b++;break;            case 3:c++;break;        }    }    int ans=0;    for(i=0;i<n;i++)    {        int lo=A[i].loc;        int v=A[i].val;        if(v==1)        {            if(lo<a)                continue;            else            {                int ele,ele2,flag=0;                if(lo>=a&&lo<b)                    ele=2,ele2=3;                else                    ele=3,ele2=2;                for(j=0;j<a;j++)                {                    if(A[j].val==ele)                    {                        swap(A[j].val,A[i].val);                        flag=1;                        ans++;                        break;                    }                }                if(!flag)                {                    for(j=0;j<a;j++)                    {                        if(A[j].val==ele2)                        {                            swap(A[j].val,A[i].val);                            ans++;                            break;                        }                    }                }            }        }        else if(v==2)        {            if(lo>=a&&lo<a+b)                continue;            else            {                int ele,ele2,flag=0;                if(lo<a)                    ele=1,ele2=3;                else                    ele=3,ele2=1;                for(j=a;j<a+b;j++)                {                    if(A[j].val==ele)                    {                        swap(A[j].val,A[i].val);                        flag=1;                        ans++;                        break;                    }                }                if(!flag)                {                    for(j=a;j<a+b;j++)                    {                        if(A[j].val==ele2)                        {                            swap(A[j].val,A[i].val);                            ans++;                            break;                        }                    }                }            }        }        else        {            if(lo>=a+b)                continue;            else            {                int ele,ele2,flag=0;                if(lo<a)                    ele=1,ele2=2;                else                    ele=2,ele2=1;                for(j=a+b;j<a+b+c;j++)                {                    if(A[j].val==ele)                    {                        swap(A[j].val,A[i].val);                        flag=1;                        ans++;                        break;                    }                }                if(!flag)                {                    for(j=a+b;j<a+b+c;j++)                    {                        if(A[j].val==ele2)                        {                            swap(A[j].val,A[i].val);                            ans++;                            break;                        }                    }                }            }        }    }    fprintf(fout,"%d\n",ans);    return 0;}




0 0
原创粉丝点击