当前位置:  编程技术>c/c++/嵌入式

贪心算法 WOODEN STICKS 实例代码

    来源: 互联网  发布时间:2014-10-15

    本文导语:  Problem DescriptionThere is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are ...

Problem Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if ll) return 1;
     else if(a->l < b->l) return -1;
     else return a->w > b->w ? 1 : -1;
 }
 int main()
 {
     int t,n,cnt,cl,cw;
     int i,j;
     scanf("%d",&t);
     while(t--)
     {
         memset(sticks,0,sizeof(sticks));
         scanf("%d",&n);
         for( i = 0; i < n; i++)
             scanf("%d %d",&sticks[i].l,&sticks[i].w);
         qsort(sticks,n,sizeof(sticks[0]),cmp);
         sticks[0].flag = 1;
         cl = sticks[0].l;
         cw = sticks[0].w;
         cnt = 1;
         for( j = 1; j < n; j++)
         {
             for( i = j; i < n; i++)
             {
                 if(!sticks[i].flag&&sticks[i].l>=cl&&sticks[i].w>=cw)
                 {
                     cl = sticks[i].l;
                     cw = sticks[i].w;
                     sticks[i].flag = 1;
                 }
             }
             i = 1;
             while(sticks[i].flag)
                i++;
             j = i;
             if(j == n) break;
             cl = sticks[j].l;
             cw = sticks[j].w;
             sticks[j].flag = 1;
             cnt++;
         }
         printf("%dn",cnt);

     }
     return 0;
 }

题意:

我们要处理一些木棍,第一根的时间是1分钟,另外的,在长度为l,重为w的木棍后面的那根的长度为l', 重量w',只要l


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 用贪心法求解背包问题的解决方法


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3