Topic links: https://ac.nowcoder.com/acm/contest/1014/A
Title Description
Freda and rainbow reared N kittens, this day, the kittens go hiking. Has passed away, the cat finally climbed the hill, but they do not want to weary and walk down the mountain (woo goo> _ <).
Enter a description:
Freda and rainbow had to spend money to make them sit down cable. The maximum loading on the cable car cableway weight is W, the weight of the N kittens are a C1, C2 ... CNC_1, C_2 \ DOTS C_N C . 1 , C 2 ... C N . Of course, the weight of the cat on the cable car and not exceed per W. Each of renting a cable car, Freda and rainbow have to pay $ 100, so they want to know, how many dollars to pay a minimum of N these kittens are transported down the mountain?
Output Description:
The first line contains two space-separated integers, N and W.
Next, an integer N lines, where the i + 1 row represents an integer of i-th kittens wt CiC_i C i .
Example 1
Entry
5 1996
1
2
1994
12
29
Export
2
prime ring with a considerable carrot a pit, the pit is a kitten climbing plurality radish, determined by the minimum number of pits. And prime ring is searching for each pit, the first step to see which several pit put;
kitten climbing is traversed to find pit for him for the first step by the number, there is no need for the re-establishment of a pit.
It is worth mentioning that the written if (cnt> res) in the minus branch by submitting a written test is much slower than if (cnt> = res), former time in about 300ms, which is 4ms; Thus, pruning it important,
proper pruning is more important.
AC code is as follows:
. 1 #include <cstdio> 2 #include <algorithm> . 3 #define Max 0x3f3f3f3f . 4 the using namespace STD; . 5 . 6 int n-, W, RES, ANS [ 20 is ], PIT [ 25 ]; . 7 . 8 BOOL CMP ( int A, int B) { . 9 return a> B; 10 } . 11 // STEP current process is the first of several cat, cnt processing the plurality of step-1 cat car 12 is void DFS ( int STEP, int CNT) { 13 is IF (CNT > =RES) 14 return ; 15 IF (step n-== + . 1 ) { 16 RES = min (RES, CNT); . 17 return ; 18 is } . 19 20 is // car in the car looking for the conventional step of cats 21 for ( int I = . 1 ; I <= CNT; I ++ ) { 22 is IF (PIT [I] + ANS [STEP] <= W) { 23 is PIT [I] + = ANS [STEP]; 24 DFS (STEP + . 1 , CNT); 25 PIT [I] - = ANS [STEP]; 26 is } 27 } 28 // find a suitable vehicle in a conventional car new one, and into which the cat 29 PIT [CNT + . 1 ] = ANS [STEP]; 30 DFS (+ STEP . 1 , CNT + . 1 ); 31 is } 32 33 is int main ( void ) 34 is { 35 Scanf ( " % D% D " , & n-, & W); 36 for ( int I = . 1 ; I <= n-; I ++ ) 37 [ Scanf ( " % D " , &ans[i]); 38 sort(ans+1,ans+1+n,cmp); 39 res = Max; 40 dfs(1,1); 41 printf("%d\n",res); 42 43 return 0; 44 }