【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 104853
  • 排名 - 233

最新评论

阅读排行榜

评论排行榜

农民John准备建一个栅栏来围住他的牧场。他已经确定了栅栏的形状,但是他在木料方面有些问题。当地的杂货储存商扔给John一些木板,而John必须从这些木板中找出尽可能多所需的木料。

当然,John可以切木板。因此,一个9英尺的木板可以切成一个5英尺和一个4英尺的木料 (当然也能切成3个3英尺的,等等)。John有一把梦幻之锯,因此他在切木料时,不会有木料的损失。

所需要的木料规格都已经给定。你不必切出更多木料,那没有用。

格式
PROGRAM NAME: fence8
INPUT FORMAT:(file fence8.in)

第1行: N (1 <= N <= 50), 表示提供的木板的数目

第2行到第N+1行: N行,每行包括一个整数,表示各个木板的长度。

第N+2行: R (1 <= R <= 1023), 所需木料的数目

第N+3行到第N+R+1行: R行,每行包括一个整数(1 <= ri <= 128)表示所需木料的长度。

OUTPUT FORMAT:(file fence8.out)

只有一行,一个数字,表示能切出的最多的所需木料的数目。当然,并不是任何时候都能切出所有所需木料。

SAMPLE INPUT
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30

SAMPLE OUTPUT
7

借鉴:nocow的分析和优化技巧

【参考程序】:

/*
ID: XIONGNA1
PROG: fence8
LANG: C++
*/
#include
<iostream>
#include
<cstring>
using namespace std;
int n,nr,sumb,ans;
int b[51],sum[1024],r[1024];
int cmp(const void *s,const void *t)
{
    
int i=*(int *)s,j=*(int *)t;
    
return i-j;
}
bool dfsid(int left,int x,int m)
{
    
int ni,nleft;
    
if (x==0return true;
    
for (int i=m;i<=n;i++)
        
if (b[i]>=r[x])
        {
            b[i]
-=r[x];
            
if (b[i]<r[1])
            {
                nleft
=left+b[i];
                
if (nleft+sum[ans]>sumb)
                {
                    b[i]
+=r[x]; continue;
                }
            }
            
else nleft=left;
            
if (r[x]==r[x-1]) ni=i;
            
else ni=1;
            
if (dfsid(nleft,x-1,ni)) return true;
            b[i]
+=r[x];
        }
    
return false;
}
int main()
{
    freopen(
"fence8.in","r",stdin);
    freopen(
"fence8.out","w",stdout);
    scanf(
"%d",&n);
    sumb
=0;
    
for (int i=1;i<=n;i++)
    {
        scanf(
"%d",&b[i]);
        sumb
+=b[i];
    }
    qsort(b
+1,n,sizeof(int),cmp);
    scanf(
"%d",&nr);
    
for (int i=1;i<=nr;i++) scanf("%d",&r[i]);
    qsort(r
+1,nr,sizeof(int),cmp);
    memset(sum,
0,sizeof(sum));
    ans
=nr;
    
for (int i=1;i<=nr;i++)
    {
        sum[i]
+=sum[i-1]+r[i];
        
if (sum[i]>sumb)
        {
            ans
=i-1break;
        }
    }
    
while (!dfsid(0,ans,1)) ans--;
    printf(
"%d\n",ans);
    
return 0;
}
posted on 2009-07-27 14:55 开拓者 阅读(601) 评论(0)  编辑 收藏 引用 所属分类: USACO 题解

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理