zoukankan      html  css  js  c++  java
  • USACO / Sorting a ThreeValued Sequence (简单题,方法正确性待证)

    三值的排序

    IOI'96 - Day 2 

      
    排序是一种很频繁的计算任务。现在考虑最多只有三值的排序问题。一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌序的时候。
      
    在这个任务中可能的值只有三种1,2和3。我们用交换的方法把他排成升序的。
      
    写一个程序计算出,给定的一个1,2,3组成的数字序列,排成升序所需的最少交换次数。
      
    PROGRAM NAME: sort3
    INPUT FORMAT
    Line 1: 
    N (1 <= N <= 1000)

     

    Lines 2-N+1: 

    每行一个数字,共N行。(1..3)

      

    SAMPLE INPUT (file sort3.in) 

      9
      2
      2
      1
      3
      3
      3
      2
      3
      1
    OUTPUT FORMAT
    共一行,一个数字。表示排成升序所需的最少交换次数。
      
    SAMPLE OUTPUT (file sort3.out)

      4

     

    简单模拟题,先把1放到1的位置,再把2放到该放的位置,剩下的3就已经放好了。看这样需要走多少步。 方法正确性不会证。。。

     

    /*
    ID: 138_3531
    LANG: C++
    TASK: sort3
    */
    
    
    #include<fstream>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    
    
    using namespace std;
    
    
    ifstream fin("sort3.in");
    ofstream fout("sort3.out");
    
    
    int main()
    {
        int a[100000];
        int n,n1=0,n2=0;
        int ans=0;
        fin>>n;
        for (int i=0;i<n;i++)
        {
            fin>>a[i];
            if (a[i]==1) n1++;
            if (a[i]==2) n2++;
        }
        for (int i=0;i<n1;i++)
            if (a[i]!=1) ans++;
        for (int i=n1;i<n1+n2;i++)
            if (a[i]==3) ans++;
        fout<<ans<<endl;
        return 0;
    }

     

    举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
  • 相关阅读:
    读入输出优化模板
    HDU-2647 Reward(拓扑排序)
    HDU-2647 Reward(拓扑排序)
    HDU-2647 Reward(拓扑排序)
    HDU-2647 Reward(拓扑排序)
    Using KafkaBolt to write to a kafka topic
    Using KafkaBolt to write to a kafka topic
    Using KafkaBolt to write to a kafka topic
    Using KafkaBolt to write to a kafka topic
    getElementById() 获取指定ID的第一个元素
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/2598495.html
Copyright © 2011-2022 走看看