posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          USACO 1.2.1 Sorting A Three-Valued Sequence

          Posted on 2007-06-11 22:00 ZelluX 閱讀(398) 評論(0)  編輯  收藏 所屬分類: Algorithm
          原來vim的復制快捷鍵是 " +y ,其中+也是要按的,怪不得以前一直不成功

          /*

          PROG: sort3
          ID: 06301031
          LANG: C++
          */

          #include 
          <fstream>
          #include 
          <iostream>
          #include 
          <vector>

          using namespace std;

          int main()
          {
              ifstream fin(
          "sort3.in");
              ofstream fout(
          "sort3.out");
              
          int n;
              fin 
          >> n;
              vector
          <int> nums(n);
              
          int numCount[4];
              numCount[
          1= numCount[2= numCount[3= 0;
              
          for (int i = 0; i < n; i++)
              {
                  fin 
          >> nums[i];
                  numCount[nums[i]]
          ++;
              }
              numCount[
          2+= numCount[1];
              numCount[
          3+= numCount[2];
              
          int toBeChanged[4][4];
              
          for (int i = 1; i <= 3; i++)
                  
          for (int j = 1; j <= 3; j++)
                      toBeChanged[i][j] 
          = 0;
              
          for (int i = 0; i < n; i++)
              {
                  
          int sortedNum = 1;
                  
          for (int j = 1; j < 4; j++)
                      
          if (i - numCount[j] < 0)
                      {
                          sortedNum 
          = j;
                          
          break;
                      }
                  cout 
          << nums[i] << ":" << sortedNum << endl;
                  
          if (nums[i] != sortedNum)
                      toBeChanged[nums[i]][sortedNum]
          ++;
              }

              
          for (int i = 1; i <= 3; i++)
                  
          for (int j = 1; j <= 3; j++)
                      
          if (toBeChanged[i][j] != 0)
                          cout 
          << i << "," << j << ":" << toBeChanged[i][j] << endl;
              
          int triangles = 0;
              
          int swaps = 0;
              
          for (int i = 1; i <= 3; i++)
                  
          for (int j = 1; j <= 3; j++)
                      
          if (toBeChanged[i][j] != 0)
                      {
                          
          int smaller = toBeChanged[i][j];
                          
          if (toBeChanged[j][i] < smaller)
                              smaller 
          = toBeChanged[j][i];
                          toBeChanged[i][j] 
          -= smaller;
                          toBeChanged[j][i] 
          -= smaller;
                          swaps 
          += smaller;
                      }
              
          for (int i = 1; i <= 3; i++)
                  
          for (int j = 1; j <= 3; j++)
                      
          if (toBeChanged[i][j] != 0)
                          triangles 
          += toBeChanged[i][j];
              cout 
          << swaps << endl << triangles << endl;
              swaps 
          += (triangles / 3 * 2);
              fout 
          << swaps << endl;
              
          return 0;
          }


          主站蜘蛛池模板: 华容县| 东至县| 宜章县| 亳州市| 辽中县| 石棉县| 图木舒克市| 长丰县| 建瓯市| 文成县| 平顶山市| 丹巴县| 皋兰县| 奉节县| 尤溪县| 邯郸县| 东光县| 天峻县| 夹江县| 巴林左旗| 佛教| 武陟县| 体育| 黄骅市| 阳山县| 涞源县| 广水市| 洮南市| 宜丰县| 顺昌县| 南城县| 英山县| 蓝山县| 宜宾市| 纳雍县| 卢氏县| 遂平县| 建阳市| 宜兰市| 板桥市| 大渡口区|