Burch, Kolstad, and Schrijvers
Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber store has dropped off boards of varying lengths; Farmer John must create as many of the rails he needs from the supplied boards.
Of course, Farmer John can cut the boards, so a 9 foot board can be cut into a 5 foot rail and a 4 foot rail (or three 3 foot rails, etc.). Farmer John has an `ideal saw', so ignore the `kerf' (distance lost during sawing); presume that perfect cuts can be made.
The lengths required for the rails might or might not include duplicates (e.g., a three foot rail and also another three foot rail might both be required). There is no need to manufacture more rails (or more of any kind of rail) than called for the list of required rails.
PROGRAM NAME: fence8
INPUT FORMAT
Line 1: | N (1 <= N <= 50), the number of boards |
Line 2..N+1: | N lines, each containing a single integer that represents the length of one supplied board |
Line N+2: | R (1 <= R <= 1023), the number of rails |
Line N+3..N+R+1: | R lines, each containing a single integer (1 <= ri <= 128) that represents the length of a single required fence rail |
SAMPLE INPUT (file fence8.in)
4 30 40 50 25 10 15 16 17 18 19 20 21 25 24 30
OUTPUT FORMAT
A single integer on a line that is the total number of fence rails that can be cut from the supplied boards. Of course, it might not be possible to cut all the possible rails from the given boards.SAMPLE OUTPUT (file fence8.out)
7
/*
ID: conicoc1
LANG: C
TASK: fence8
*/
/*迭代加深搜索+二分优化*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Min(a,b) a>b?b:a
int BoardCount,BoardSum=0;
int RailCount,RailSum=0;
int Board[50];
int Rail[1023];
int flag[1023];
int MinRail=129;
int NoUseBoard=0;
int comp1(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int comp2(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int DFSID(int *TmpBoard,int Depth,int BoardNumber) //BoardNumber为上一个木材所使用的木块编号
{
if(Depth<0)
return 1;
int i,NoUseBoard=0;
/*********剪枝1********/
if(BoardNumber!=-1&&Rail[Depth]==Rail[Depth+1])
i=BoardNumber;
else
i=0;
/*********************/
for(;i<BoardCount;i++){
/*********剪枝2********/
if(TmpBoard[i]+MinRail>Board[i])
NoUseBoard+=Board[i]-TmpBoard[i];
if(NoUseBoard>BoardSum-RailSum)
return 0;
/***********************/
if(TmpBoard[i]+Rail[Depth]<=Board[i]){
TmpBoard[i]+=Rail[Depth];
if(DFSID(TmpBoard,Depth-1,i)){
TmpBoard[i]-=Rail[Depth];
return 1;
};
TmpBoard[i]-=Rail[Depth];
}
}
return 0;
}
int main()
{
FILE *fin,*fout;
fin=fopen("fence8.in","r");
fout=fopen("fence8.out","w");
int i,TmpBoard[50],Left,Right,Mid;
memset(TmpBoard,0,sizeof(TmpBoard));
memset(flag,0,sizeof(flag));
fscanf(fin,"%d",&BoardCount);
for(i=0;i<BoardCount;i++){
fscanf(fin,"%d",&Board[i]);
BoardSum+=Board[i];
}
fscanf(fin,"%d",&RailCount);
for(i=0;i<RailCount;i++){
fscanf(fin,"%d",&Rail[i]);
MinRail=Min(MinRail,Rail[i]);
}
qsort(Rail,RailCount,sizeof(int),comp1);
qsort(Board,BoardCount,sizeof(int),comp2);
/*排除过于小的Board,或者过于大的Rail*/
while(RailCount>0&&Rail[RailCount-1]>Board[0])
RailCount--;
while(BoardCount>0&&Board[BoardCount-1]<Rail[0])
BoardCount--;
Left=-1;
Right=RailCount;
while(Left+1<Right){
Mid=(Left+Right)/2;
for(RailSum=0,i=0;i<=Mid;i++){
RailSum+=Rail[i];
}
if(DFSID(TmpBoard,Mid,-1))
Left=Mid;
else
Right=Mid;
}
fprintf(fout,"%d\n",Left+1);
return 0;
}
一开始想用01背包的DP来解,发现想不出可行的DP方程。
二分+DFSID(迭代加深搜索),相比BFS来说节约了空间复杂度,时间复杂度经过验证应该是差不多的。
接近一个月没有更新了,在学校里敲代码就不想更新博客,现在正好回顾一遍。