UVa 10664 Luggage (0-1背包)

來源:互聯網
上載者:User

10664 - Luggage

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1605

Peter and his friends are on holiday, so they have decided to make a trip by car to know the north of Spain. They are seven people and they think that two cars are enough for their luggage.

It’s time to leave… and a heap of suitcases are awaiting out of the cars. The drivers disagree about which suitcase must be put into each boot, because nobody wants one boot to carry more weight than the other one. Is it possible that the two boots load with the same weight? (Obviously without unpacking the suitcases!)

Consider m sets of numbers representing suitcases weights, you must decide for each one, if it is possible to distribute the suitcases into the boots, and the two boots weigh the same.

Input

The first line of the input contains an integer, m, indicating the number of test cases. For each test case, there is a line containing n integers (1<=n<=20) separated by single spaces. These integers are the weights of each suitcase. The total sum of the weights of all the suitcases is less or equal to 200 kilograms.

Output

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45376.htm

The output consists of m lines. The i-th line corresponds with the i-th set of suitcases weight and contains the string “YES” or “NO”, depending on the possibility that the two boots load with the same weight for the respective test case.

31 2 1 2 12 3 4 1 2 5 10 50 3 503 5 2 7 1 7 5 2 8 9 1 25 15 8 3 1 38 45 8 1
NOYESYES

水一水。

完整代碼:

/*0.015s*/    #include<cstdio>  #include<cstring>  #include<algorithm>  using namespace std;      int val[25], dp[205];      int main()  {      int t, sum, n, i, j;      char ch;      scanf("%d", &t);      while (t--)      {          sum = n = 0;          do        {              scanf("%d", &val[n]);              sum += val[n++];              ch = getchar();          }          while (ch != 10 && ch != -1);          if (sum & 1) puts("NO");          else        {              memset(dp, 0, sizeof(dp));              sum >>= 1;              for (i = 0; i < n; ++i)                  for (j = sum; j >= val[i]; --j)                      dp[j] = max(dp[j], dp[j - val[i]] + val[i]);              puts(dp[sum] == sum ? "YES" : "NO");          }      }      return 0;  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.