Codeforces B. Taxi 演算法題解

來源:互聯網
上載者:User

簡單總結題意:

有一個數組,數組中的數值不會大於4,即只能是1,2,3,4,要把這些數值裝進一個容量最大為4的容器裡面,使得所用到這樣的容器的個數最小。

經測試資料很大,會有10萬個資料,所以這裡我並不用排序資料,而是使用counting sort的思想,根據特定的資料最佳化,使得題解時間複雜度為O(n)。

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

程式如下:

#include <iostream>  #include <vector>  #include <string>  #include <ctype.h>        using namespace std;        void Taxi()  {      int n = 0;      cin>>n;      int nums = 0;      int A[3] = {0};      int a = 0;      for (int i = 0; i < n; i++)      {          cin>>a;          if (4 == a) nums++;          else A[a-1]++;      }            nums += A[1] / 2;      A[1] %= 2;            if (A[0] <= A[2])       {          nums += A[0];          A[2] -= A[0];          if (A[1]) nums++;          nums += A[2];      }      else    {          nums += A[2];          A[0] -= A[2];          nums += A[0] / 4;          A[0] %= 4;          if (A[1])          {              A[0] -= 2;              nums++;          }          if (A[0] > 0) nums++;      }            cout<<nums;  }

題目出處:

http://codeforces.com/problemset/problem/158/B

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.