[2329] Luo Gu fence

Title Description

Farmer John intends to build a fence around his ranch to them, so he needs some special specifications of the timber. So Farmer John to the store to buy wood timber. But the wood shop owner said he was here only a part of the large size of the board under less. But John can buy these boards, then cut to the specifications he needs. And John has a magic saw, use it to saw wood, without any loss, that is to say a length of wood can be cut to a length of 10 two planks 8 and 2.

Your mission: to John you need wood specifications, as well as wood shop owner can give the timber specifications, find the maximum number of planks John he needs to be able to get.

Input Format

The first line integer m (m <= 50) represents a wood shop owner how many pieces of wood can be provided to John. Followed by the length of each piece of wood m behavior provided by the boss.

The next line (i.e., the second row m + 2) is an integer of n (n <= 1000), John indicates how many timber needs.

Next n lines represents the length he needed each piece of wood. wood

#include <cstdio> 
#include <the iostream> 
#include <the cmath> 
#include <CString> 
#include <the cstdlib> 
#include <algorithm> 
#include <Queue>
 the using  namespace STD;
 const  int N = 1050 ;
 int A [N] , B [N], C [N], n-, MID, m, W, TOT, SUM [N], ANS; 

BOOL Check ( int H, int Last) {
     BOOL FF = to false ;
     IF (TOT-W <SUM [MID]) return  0 ; // pruning, if the total waste minus <1 ~ mid Total required, would certainly not. 
    if(h==1) return 1;
    for(int i=last;i<=m;i++){
        if(a[i]-b[h]>=0){
            a[i]-=b[h];
            if(a[i]<b[1]) w+=a[i];
            
            if (b[h]==b[h-1]) ff=check(h-1,i);
            else ff=check(h-1,1);
            
            if(a[i]<b[1]) w-=a[i];
            a[i]+=b[h];
            
            if(ff==true) Return  . 1 ; // if you try to return after the possible, it returns to true 
        } 
    } 
    return  0 ; 
} 

int main () { 
    Scanf ( " % D " , & n-);
     for ( int I = . 1 ; I <= n-; I ++ ) { 
        Scanf ( " % D " , & A [I]); 
        TOT + = A [I]; C [I] = A [I]; 
    } 
    Scanf ( " % D " , & m);
     for ( int I = . 1;i<=m;i++){
        scanf("%d",&b[i]);
    }
    sort(b+1,b+m+1);
    for(int i=1;i<=m;i++)
        sum[i]=sum[i-1]+b[i];
    while(sum[m]>tot) m--;
    int l=1,r=m;
    while(l<=r){
        w=0; mid=(l+r)/2;
        for(int i=1;i<=n;i++) a[i]=c[i];
        if(check(mid,1)==1) { ans=mid; l=mid+1; }
        else r=mid-1;
    }
    if(l==2) printf("0\n");
    else printf("%d\n",l-1);
    
    return 0;
}

 

Dimension lumber is less than 32767. (For shop owner offered each board and John need, you can only use once).

Output Format

Only one line, the number for John can get the most qualified wood.

Sample input and output

Input # 1
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
Output # 1
7
Input # 2
3
20
10
10
9
3
3
3
5
5
7
8
8
9
Output # 2
7 

problem solution: DFS + pruning + binary search (not too hard but the question is purple hehe.)


Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11694387.html