/* -------------------------------------------------------------- There are n people, the I-th weight is wi, the maximum load of each ship is C, and the maximum load can only take two people. Use the least ship to load the owner. Input: the first line has two integers, N and N, respectively, indicating wi output. The first line outputs the number of ships. Num. The second line outputs two numbers to the n + 1 line, the "Person's ID" and the "ship's ID" respectively ". The numbers of people are numbered from 1 to n in the input order. The ship number starts from 1. Rows 2nd to n + 1 are output in ascending order of numbers. Thought: the lightest person I. Who should he be on a ship? If everyone cannot take a boat with him, the only solution is to build a boat for everyone. Otherwise, he should choose the heaviest J of the person who can take a boat with him. This method is greedy, and he can ensure that the "present" waste is the least. Therefore, the program first sorts by weight, and then uses I and j to represent the lightest and heaviest people currently under consideration. Move J to the left each time until I and j can take a ship together, then I add 1, J minus 1, and repeat the above operation. Time complexity mainly depends on sorting. The complexity of the preceding scan is O (n. ----------------------------------------------------------------*/
1 # include <stdio. h> 2 # include <stdlib. h> 3 struct person 4 {5 Int ID; // The number of the person 6 double weight; 7 int shipid; // The ship number 8 }; 9 int cmpqsort1 (const void * a, const void * B); // compare the values of A and B according to the weight of struct person. Comparison of positive direction. 10 int cmpqsort2 (const void * a, const void * B); // compare the size of a and B According to the struct person ID. Comparison of positive direction. 11 int main () 12 {13 int N, C, I, j, T; // t indicates the ID 14 int flag = 0 to be allocated to the ship of the I-th individual; // It indicates that no one is overweight and cannot take a boat alone. 15 struct person * A; 16 freopen ("5.in"," r ", stdin); 17 scanf (" % d ", & N, & C ); 18 A = (struct person *) malloc (sizeof (struct person) * n); 19 for (I = 0; I <n; I ++) 20 {21 scanf ("% lf", & A [I]. weight); 22 A [I]. id = I + 1; 23 A [I]. shipid = 0; 24} 25 qsort (A, N, sizeof (struct person), cmpqsort1); 26 T = 1; 27 for (I = 0, j = n-1; I <= J; I ++, j --) // analyze the I <= J. 28 {29 if (a [I]. weight> C) 30 {31 printf ("input error! Some people weigh too much, even if they are sitting alone in a boat ...... \ N "); 32 flag = 1; // indicates that someone has been found to be too heavy to take a boat alone. 33 break; 34} 35 else36 {37 A [I]. shipid = T; 38 While (A [I]. weight + A [J]. weight> C & J> I) j --; 39 A [J]. shipid = T; 40 t ++; 41} 42} 43 If (flag = 0) 44 {45 for (; I <n; I ++) // orchestrate the 46 {47 if (a [I]. shipid = 0) // if this person does not have an orchestration ship number 48 {49 if (a [I]. weight <c) 50 {51 A [I]. shipid = T; 52 T ++; 53} 54 else55 {56 printf ("incorrect input! Some people weigh too much, even if they are sitting alone in a boat ...... \ N "); 57 flag = 1; // indicates that someone has been found to be too heavy to take a boat alone. 58 break; 59} 60} 61} 62 if (flag = 0) 63 {64 qsort (A, N, sizeof (struct person), cmpqsort2 ); 65 printf ("% d \ n", t-1); 66 for (I = 0; I <n; I ++) // output result 67 {68 printf ("%-8.2lf %-3D %-3D \ n", a [I]. weight, a [I]. ID, a [I]. shipid); 69} 70} 71} 72 free (a); 73 return 0; 74} 75 int cmpqsort1 (const void * a, const void * B) // compare the sizes of A and B according to the weight of struct person. Comparison of positive direction. 76 {77 double T = (struct person *) A)-> weight-(struct person *) B)-> weight; 78 If (T> 1e-5) return 1; 79 else if (T <(-1e-5) Return-1; 80 else return 0; 81} 82 int cmpqsort2 (const void * a, const void * B) // compare the size of a and B According to the struct person ID. Comparison of positive direction. 83 {84 int t = (struct person *) A)-> ID-(struct person *) B)-> ID; 85 if (T> 1e-5) return 1; 86 else if (T <(-1e-5) Return-1; 87 else return 0; 88}
View code
Running case
Input:
10 150
80
89
70
30
45
100
120
50
120
40
Output:
6
80.00 1 4
89.00 2 3
70.00 3 5
30.00 4 1
45.00 5 3
100.00 6 2
120.00 7 1
50.00 8 4
120.00 9 6
40.00 10 2
Press any key to continue...