Two ships carrying things

Source: Internet
Author: User
Question
Question: Given the weight of N items and the two ships carrying weight respectively C1 and C2, can we use these two ships to hold all the items. Input: the input contains multiple groups of test data. Each group of test data consists of several rows of data. The first behavior has three integers: N C1 C2, (1 <= n <= 100), (1 <= C1, C2 <= 5000 ). Next n rows, each row has an integer representing the weight of each item (the weight is not greater than 100 ). Output: for each group of test data, if only the two ships can be used to hold all the items, the output is yes. Otherwise, no is output. Sample input: 3 5 86333 5 8534 sample output: Noyes
The first thought (error) was to consider greedy algorithms. Every time I found the heaviest cargo, I wrote a test code:
# Include <stdio. h> # include <stdlib. h> int cmp_data (const void * P, const void * q) {const int * A = P; const int * B = Q; return * B-* ;} int main (void) {int I, n, C1, C2, * arr; while (scanf ("% d", & N )! = EOF) {scanf ("% d", & C1, & C2); arr = (int *) malloc (sizeof (INT) * n ); for (I = 0; I <n; I ++) scanf ("% d", & arr [I]); qsort (ARR, n, sizeof (ARR [0]), cmp_data); // greedy algorithm int flag; for (I = 0, flag = 1; I <n; I ++) {If (C1> = arr [I]) {C1-= arr [I];} else if (c2> = arr [I]) {C2-= arr [I] ;}else {flag = 0; break ;}}if (FLAG) {printf ("Yes \ n ");} else {printf ("NO \ n");} Free (ARR);} return 0 ;}
However, it is obvious that this approach is wrong. The reason is to give a counterexample: 3, 5, 5, 4, and the correct train of thought, in fact, there is no need to consider the issue of two ships, consider how much cargo a ship can carry at most. Suppose the maximum carrying capacity is W, and then check whether the total weight sum-W is smaller than the carrying capacity of the second ship. It is a 0-1 backpack problem AC code.
# Include <stdio. h> # include <stdlib. h> int cmp_data (const void * P, const void * q) {const int * A = P; const int * B = Q; return * A-* B ;} int main (void) {int I, j, N, C1, C2, sum, * arr, ** DP; while (scanf ("% d", & N )! = EOF) {scanf ("% d", & C1, & C2); arr = (int *) malloc (sizeof (INT) * (n + 1 )); arr [0] = 0; for (I = 1, sum = 0; I <= N; I ++) {scanf ("% d ", & arr [I]); sum + = arr [I];} qsort (ARR, N, sizeof (ARR [0]), cmp_data ); // 0-1 backpack dp = (INT **) malloc (sizeof (int *) * (n + 1); for (I = 0; I <= N; I ++) DP [I] = (int *) malloc (sizeof (INT) * (C1 + 1); for (I = 0; I <= C1; I ++) DP [0] [I] = 0; for (I = 0; I <= N; I ++) DP [I] [0] = 0; for (I = 1; I <= N; I ++) {for (j = 1; j <= C1; j ++) {If (ARR [I]> J) {DP [I] [J] = DP [I-1] [J];} else {DP [I] [J] = (DP [I-1] [J]> arr [I] + dp [I-1] [J-Arr [I] )? DP [I-1] [J]: arr [I] + dp [I-1] [J-Arr [I] ;}} if (Sum-DP [N] [C1] <= c2) {printf ("Yes \ n");} else {printf ("NO \ n ");} free (ARR);} return 0 ;} /*************************************** * *********************** problem: 1462 User: wangzhengyi language: C result: accepted time: 20 MS memory: 11344 kb ************************************** **************************/
I have a classmate I don't know, 9 degrees OJ, And I was squeezed out just before entering the first 10 last night !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.