USACO 2.1 Sorting a Three-Valued Sequence

题目原文

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 most three 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 sorted
Lines 2-N+1:A single integer from the set {1, 2, 3}

SAMPLE INPUT (file sort3.in)

9
2
2
1
3
3
3
2
3
1

OUTPUT FORMAT

A single line containing the number of exchanges required

SAMPLE OUTPUT (file sort3.out)

4

分析

题目的意思非常简单,给出一系列集合(1,2,3)中的数字,为了按增序排序,需要最少的数字交换次数。例如,输入的例子最少需要下面四次交换才可以达到增序排序。
2	1	1	1	1
2	2    	2	1	1
1	2    	2	2	2
3	3	2	2	2
3	3	3	3	2
3	3	3	3	3
2	2	3	3	3
3	3	3	3	3
1	1	1	2	3
由于输入的数字都在集合(1,2,3)之内,因此输入完成后可以直接得到排序的最终状态,比较最终状态和初始状态就可以得到交换次数了。通过观察上面的例子,可以发现,数字交换的主要有交换一次实现(前两步)和交换两次实现(后两步)两种状况。在第一步交换中,前两个‘2’处在最终状态‘1’的位置,而‘1’处在最终状态‘2’的位置,这里只需要交换一步,第二步交换类似,是将处在最终状态‘3’中的2与处在状态‘2’中的‘3’进行交换;第三步和第四步是为了将处在3里的1交换到当前正被2占据的位置,这一过程需要两步。
总结起来可以用下面的公式描述最少交换次数
swaptime = min(_1at2,_2at1) + min(_2at3,_3at2) + min(_1at3,_3at1) + 2* (max(_2at1,_1at2)-min(_2at1,_1at2));

提交代码

/*
ID: Aaron Cai
PROG: sort3
LANG: C++
*/



#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
#include <map>
using namespace std;

int main()
{
	ifstream fin("sort3.in");
	ofstream fout("sort3.out");


	int N; fin>>N;

	vector<int> data(N);
	vector<int> count(3,0);
	for (int i=0;i!=N;i++)
	{
		fin >> data[i];;
		count[data[i]-1]++;
	}

	int _1at2=0;
	int _1at3=0;
	int _2at1=0;
	int _2at3=0;
	int _3at1=0;
	int _3at2=0;

	for (int i=0;i!=count[0];i++)
	{
		if(data[i] == 2)
			_2at1++;
		if(data[i] == 3)
			_3at1++;
	}
	for (int i=count[0];i!=count[0]+count[1];i++)
	{
		if(data[i] == 1)
			_1at2++;
		if(data[i] == 3)
			_3at2++;
	}
	for (int i=count[0]+count[1];i!=N;i++)
	{
		if(data[i] == 1)
			_1at3++;
		if(data[i] == 2)
			_2at3++;
	}

	int swaptime = min(_1at2,_2at1) + min(_2at3,_3at2) + min(_1at3,_3at1) + 2* (max(_2at1,_1at2)-min(_2at1,_1at2));
	
	fout << swaptime << endl;

	return 0;
}


提交结果

TASK: sort3
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.008 secs, 3500 KB]
   Test 2: TEST OK [0.003 secs, 3500 KB]
   Test 3: TEST OK [0.003 secs, 3500 KB]
   Test 4: TEST OK [0.008 secs, 3500 KB]
   Test 5: TEST OK [0.008 secs, 3500 KB]
   Test 6: TEST OK [0.008 secs, 3500 KB]
   Test 7: TEST OK [0.003 secs, 3500 KB]
   Test 8: TEST OK [0.005 secs, 3500 KB]

All tests OK.

官方参考答案

#include <fstream>

using namespace std;

int min (int a, int b) { return a < b ? a : b; }
int max (int a, int b) { return a > b ? a : b; }

int main () {
    int s[1024];
    int n;
    int sc[4] = {0};
    
    ifstream fin("sort3.in");
    ofstream fout("sort3.out");
    fin>>n;
    for (int i = 0; i < n; i++) {
        fin>>s[i];
        sc[s[i]]++;
    }
    int s12 = 0, s13 = 0, s21 = 0, s31 = 0, s23 = 0, s32 = 0;
    for (int i = 0; i < sc[1]; i++){
        if (s[i] == 2) s12++;
        if (s[i] == 3) s13++;
    }
    
    for (int i = sc[1]; i < sc[1]+sc[2]; i++){
        if (s[i] == 1) s21++;
        if (s[i] == 3) s23++;
    }
    
    for (int i = sc[1]+sc[2]; i < sc[1]+sc[2]+sc[3]; i++){
        if (s[i] == 1) s31++;
        if (s[i] == 2) s32++;
    }
    
    fout<<min(s12, s21)+min(s13, s31)+min(s23, s32) +
					2*(max(s12, s21) - min(s12, s21))<<endl;
    return 0;
}
官方答案除了给出本文这种思路的,还有其他方法,但都没有这种方法简单高效。这里不作赘述。

THE END


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值