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 閱讀(399) 評論(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;
          }


          主站蜘蛛池模板: 蕉岭县| 邮箱| 阿荣旗| 科技| 祥云县| 鲁山县| 兴和县| 巴彦淖尔市| 夏河县| 巴林右旗| 济南市| 左贡县| 河南省| 玉山县| 溧水县| 洪泽县| 东乌珠穆沁旗| 宁远县| 项城市| 永吉县| 石林| 岳阳县| 池州市| 大化| 金堂县| 宁化县| 阿荣旗| 郧西县| 安国市| 揭西县| 全椒县| 稷山县| 遵义市| 河间市| 双柏县| 河北省| 武夷山市| 阳东县| 固原市| 西青区| 吉木萨尔县|