题目大意:

输入N ( 1 ≤ N ≤ 20,000 ) ;将一块木板分为n块

每次切割木板的开销为这块木板的长度,即将长度为21的木板分为13和8,则开销为21

接下来n行描述每块木板要求的长度Li ( 1 ≤ Li ≤ 50,000 )

木板长度恰好等于各段木板长之和

Sample Input

3
8
5
8

Sample Output

34

挑战上的一个奇妙的贪心的方法

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int n,L[];
  5. int main()
  6. {
  7. while(~scanf("%d",&n))
  8. {
  9. memset(L,,sizeof(L));
  10. for(int i=;i<n;i++) scanf("%d",&L[i]);
  11. ll ans=; /// 从结果L[]倒推
  12. while(n>)
  13. {
  14. int i0=,i1=; // 维护最小段和次小段的下标
  15. if(L[i0]>L[i1]) swap(L[i0],L[i1]);
  16. for(int i=;i<n;i++)
  17. if(L[i]<L[i0]) i1=i0,i0=i;
  18. else if(L[i]<L[i1]) i1=i;
  19.  
  20. int tmp=L[i0]+L[i1];// 将两段合并
  21. ans+=(ll)tmp; // 开销为长度
  22.  
  23. /* 舍弃L[i0]和L[i1] 并将合并后的tmp保存在L[]中
  24. 就是总共只舍弃一段
  25. 而因为段数n随着合并会不断缩小
  26. 所以把要舍弃的一段放在n-1的位置即可
  27.  
  28. *若i0或i1有一个为n-1 则让i1为n-1
  29. 将tmp放在i0中 i1舍弃
  30. *若i0和i1都不为n-1 则L[n-1]是需要被保留的
  31. 将tmp放在i0中 L[n-1]放在i1中
  32. */
  33. if(i0==n-) swap(i0,i1);
  34. L[i0]=tmp; L[i1]=L[n-];
  35. n--;
  36. }
  37. printf("%lld\n",ans);
  38. }
  39.  
  40. return ;
  41. }

可以借助优先队列高效实现

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int n;
  5. int main()
  6. {
  7. while(~scanf("%d",&n))
  8. {
  9. priority_queue <int,vector<int>,greater<int> > q;
  10. for(int i=;i<n;i++)
  11. {
  12. int m; scanf("%d",&m);
  13. q.push(m);
  14. }
  15. ll ans=;
  16. while(q.size()>)
  17. {
  18. int i0=q.top(); q.pop();
  19. int i1=q.top(); q.pop();
  20. int tmp=i0+i1;
  21. ans+=(ll)tmp; //printf("%d %d %lld\n",i0,i1,ans);
  22. q.push(tmp);
  23. }
  24. printf("%lld\n",ans);
  25. }
  26.  
  27. return ;
  28. }

USACO 2006 November Gold Fence Repair /// 贪心(有意思)(优先队列) oj23940的更多相关文章

  1. USACO 2006 November Gold Corn Fields

    USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...

  2. USACO 2006 November Gold

    POJ 3253 Fence Repair STL堆操作 我想说,STL里堆是我目前见到最蛋疼的操作. #include <cstdio> #include <cstring> ...

  3. 【USACO 2006 November Gold】Corn Fields

    [题目链接] 点击打开链接 [算法] 状压DP [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 12 #def ...

  4. USACO 2006 November Gold Corn Fields /// 状压 oj23941

    题目大意: 输入n m 接下来n行m列 0表示不能种玉米 1表示能 要求种玉米位置的上下左右四连通区域不能种玉米 输出方案数 Sample Input 2 31 1 10 1 0 Sample Out ...

  5. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

  6. poj 3253 Fence Repair (STL优先队列)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...

  7. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  9. [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)

    题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...

随机推荐

  1. 使用Thread创建线程

    #_author:来童星#date:2019/12/17#使用Thread创建线程import threadingimport timeclass Sunthread(threading.Thread ...

  2. VS 2019企业版激活码

    Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  3. Go 关系运算符

    Go 关系运算符 package main import "fmt" func main() { var a int = 21 var b int = 10 if( a == b ...

  4. Go Hello World 实例

    ## Go Hello World 实例 package main import "fmt" func main() { /* This is my first sample pr ...

  5. NX二次开发-NXOPEN工程图导出CAD图纸DxfdwgCreator *dxfdwgCreator1;

    没有什么可以看的,NXOPEN直接录制一下导出CAD就可以了.录制出来自己挑需要的代码拿过来改一下. NX9+VS2012 #include <NXOpen/Part.hxx> #incl ...

  6. Spring-Security (学习记录二)--修改为自己的登录页面

    目录 1.修改spring-security.xml配置文件 2.增加login.jsp页面 3.重启项目即可看到效果 1.修改spring-security.xml配置文件 <!-- auto ...

  7. python从入门到大神---2、和Python编程相遇的日子

    python从入门到大神---2.和Python编程相遇的日子 一.总结 一句话总结: python2和python3是很不同的,连语法都不同,比如 print 函数打印结果 1.python中pip ...

  8. CentOS 启动提示unexpected inconsistency;RUN fsck MANUALLY, ntfs的input/output Error,InPageError c000009c使用chkdsk修复磁盘,12款Linux系统恢复工具

    CentOS这两天服务器出了问题了,提示如下: unexpected inconsistency;RUN fsck MANUALLY An error occurred during the file ...

  9. C++实现的B树

    参考资料:按第一个参考资料构建,代码基本上来自于第二个参考资料 https://www.cnblogs.com/guohai-stronger/p/9225057.html https://www.c ...

  10. CVE-2018-3246 weblogic xxe

    使用P牛2018-2894的容器 http://192.168.245.130:7001/ws_utc/begin.do 导入测试用例 上传时抓取数据包 POST /ws_utc/resources/ ...