zoukankan      html  css  js  c++  java
  • USACO Sorting a Three-Valued Sequence

    首先来看一下题目:

    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
    

    这道题的思路是这样的,首先,如果可以两两交换的,就两两交换,否则就三个轮换。

    在没有任何改变的情况下,值是这样的:

    此时所说的两两交换首先是第0组与第2组的交换,此时可以看到第0组和第2组的当前值发生了交换

    同理,第3组与第6组交换

     此时第1组、第4组、第8组的都不满足两两交换的条件,因此第1组与第4组交换,此时第4组得到了满足

    最后将第1组与第8组交换

    从上面的步骤可以看出来,如果出现了无法两两交换的元素,那么每三组需要交换两次。至于为什么这样做交换次数最少,我目前是靠的直觉。

    /**
    ID: njuwz151
    TASK: sort3
    LANG: C++
    **/
    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int maxn = 1005;
    int n;
    int number[maxn];
    int expected[maxn];
    int m_count[] = {0, 0, 0};
    int swap_time = 0;
    void search();
    
    int main() {
        freopen("sort3.in", "r", stdin);
        freopen("sort3.out", "w", stdout);
        
        cin >> n;
        for(int i = 0; i < n; i++) {
            cin >> number[i];
            m_count[number[i]-1]++;
        }
        for(int i = 0; i < m_count[0]; i++) {
            expected[i] = 1;
        }
        for(int i = m_count[0]; i < m_count[0] + m_count[1]; i++) {
            expected[i] = 2;
        }
        for(int i = m_count[0] + m_count[1]; i < n; i++) {
            expected[i] = 3;
        }
        search();
        cout << swap_time << endl;
    } 
    
    void search() {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i == j) {
                    continue;
                }
                if(expected[i] == number[j] && expected[j] == number[i] && number[i] != number[j]) {
                    swap(number[i], number[j]);
                    swap_time++;    
                }
            }
        }
        int unpair = 0;
        for(int i = 0; i < n; i++) {
            if(expected[i]!=number[i]) {
                unpair++;
            }
        }
        swap_time += unpair / 3 * 2;
    }
    
    void swap(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }
  • 相关阅读:
    在SharePoint 2010中使用jQuery
    SharePoint 2010整合Silverlight 4应用 任务管理
    在LINQ to SharePoint中使用创建时间,创建者,修改时间,修改者
    修改SharePoint页面上的控件数量的限制
    SharePoint 2010开发工具图解系列:PowerShell脚本
    SharePoint 2010:部署.resx(资源)文件到App_GlobalResources的简单方法
    使用[本人]创建视图筛选时的一个问题和解答
    在SharePoint 2010环境下区分w3wp进程
    通过PowerShell实现SharePoint列表增删改
    SharePoint 2010开发工具图解系列:Visual Studio 2010创建事件接收器
  • 原文地址:https://www.cnblogs.com/NJUWZ/p/7056309.html
Copyright © 2011-2022 走看看