The passing rate of this question is not very high.CodePaste it here for your communication!
My solution: a total of N types of candy, each a [I. The problem can be solved in this way.If there are more sweets, you can eat them first.. For example, five types of candy, the number is as follows: 5 1 1 1 1. If the number of Sweets is 1
5 is the kind of candy that can't be eaten; if you eat 5 first, you will be able to finish eating. (I personally understand that it cannot be proved exactly !!!)
Code: ① sort by heap and sort by the number of each candy in descending order a [n]. ② The judge_eat () function is used to determine whether the food can be eaten. The method is: TEM is used to record the remaining sweets for each of the two types, and the value is 0. If TEM> = A [I],
Then TEM = tem-A [I]; otherwise, TEM = A [I]-tem-1. For example, in the previous example: ① TEM = 0, a [1] = 5, a [1] will be eaten with one candy, TEM = A [I]-tem-1 = 5-0-1 = 4. ② Comparison between TEM and a [2. TEM = 4> = A [2] = 1, a [2] Sugar
The fruit will be all eaten, and the TEM will also eat a [2] (the last one is the candy in the TEM, just like I have 2 apples, you have 2, you eat first, in the end, it must be my food. The premise is that the number of apples is no less than yours. This determines
The number of apples I eat is the same as that you eat. At this time, TEM = tem-A [2]-1 = 4-1 = 3. ③ A [3] = 1, similarly, TEM = tem-A [3] = 3-A [3] = 3-1 = 2. ④ A [4] = 1, TEM = tem-A [4] = 2-1 = 1. ⑤ A [5] = 1. Finally, a [5] is eaten first, and then
Take the last one in TEM.
Courseware, the principle of eating candy: 1. Which of the following is the most popular? 2. For each comparison, the system first eats the candy in a [I, because the last time I eat candy in TEM, I can eat it when I meet the conditions of my questions.
Code:
# Include <iostream> using namespace STD; void shift (int A [], int K, int m) // adjust heap {int I = K, j = 2 * I, TEM = A [k]; bool finished = false; while (j <= M &&! Finished) {If (j <M & A [J + 1] <A [J]) J ++; If (TEM <= A [J]) finished = true; else {A [I] = A [J]; I = J; j * = 2;} A [I] = TEM;} void heap_sort (int A [], int N) // heap sorting {int I, TEM; for (I = n/2; I> = 1; I --) shift (A, I, n ); for (I = N; I> = 2; I --) {TEM = A [1]; A [1] = A [I]; A [I] = TEM; shift (A, 1, I-1) ;}} bool deal_eat (int A [], int N) // process the result of eating {int TEM = 0; For (INT I = 1; I <= N; I ++) {If (TEM <A [I]) TEM = A [I]-tem-1; elsetem-= A [I];} if (TEM = 0) return true; return false;} int buffer [1000001]; int main () {int I, n, T; CIN> T; while (t --) {CIN> N; for (I = 1; I <= N; I ++) CIN> buffer [I]; heap_sort (buffer, n); bool judge = deal_eat (buffer, n); If (judge) cout <"yes"; elsecout <"no"; cout <Endl ;} return 0 ;}