[C++]Fence Repair(贪心)

Fence Repair:
农夫约翰为了修理栅栏,要将一块很长的木板切割成N块。准备切成的木块的长度为L1, L2,…Ln,未切割前木板的长度恰好为切割后木板长度的总和。每次切割木板时,需要的开销为这块木板的长度。例如长度为21的木板要切成长度为5,8,8的三块木板。长21的木板切成长为13和8的木板时,开销为21.再将长度为13的板切成长度为5和8的板时,开销为13.于是合计开销时34.请求出按照目标要求将木板切割完最小的开销是多少。
输入格式:
Line 1: One integer N, the number of planks
Lines 2… N+1: Each line contains a single integer describing the length of a needed plank
输出格式:
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

输入:
3
8 5 8
输出:
34

解题思路:每次选择最小的两块木板相加,并将结果加入待选择木板列中,直到只剩下一块木板为止。

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;typedef long long ll;const int maxn = 20000 + 10;int n;
ll L[maxn];int main(){cin>>n;for(int i = 0; i<n; i++){cin>>L[i];}sort(L, L+n);ll sum = 0;while(n > 1){int m1 = 0, m2 = 1;if(L[m1] > L[m2]){int t = m1;m1 = m2;m2 = t;}   for(int i = 2; i<n; i++){if(L[i] < L[m1]){m2 = m1;m1 = i;} else if(L[i] < L[m2]){m2 = i;}}ll res = L[m2] + L[m1];sum += res;if(m1 == n-1){int t = m1;m1 = m2;m2 = t;}L[m1] = res;L[m2] = L[n-1];n--;}cout<<sum<<endl;return 0;
}

Fence Repair--POJ3253相关推荐

  1. Fence Repair (poj3253)

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  2. POJ3253 Fence Repair【哈夫曼树+优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48026   Accepted: 15784 De ...

  3. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

  4. 老BOJ 07 Fence Repair

    Fence Repair Accept:199     Submit:784 Time Limit:1000MS     Memory Limit:65536KB Description Farmer ...

  5. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

  6. POJ 3253 Fence Repair C++ STL multiset 可解

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 Descri ...

  7. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  8. 编程算法 - 篱笆修理(Fence Repair) 代码(C)

    篱笆修理(Fence Repair) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把一块木板切成N块, 每次切两块, 分割的开销是木板长度, ...

  9. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  10. Fence Repair(不会优先队列的看过来)

    Fence Repair(不会优先队列的看过来) Farmer John wants to repair a small length of the fence around the pasture. ...

最新文章

  1. 链表的基本操作(c++实现)
  2. mysql 查询优化实验报告_Mysql查询优化小结
  3. 速领!抗疫大礼包(含QQ音乐、全民K歌、网易云音乐等等)
  4. python使用字典实现switch_python之 利用字典与函数实现switch case功能
  5. 预装鸿蒙系统的手机,首款预装鸿蒙系统的手机入网,麒麟9000加持,型号亮了...
  6. 20181127-1 附加作业 软件工程原则的应用实例分析
  7. c语言二维数组中的周边,【C语言】二维数组中的查找,杨氏矩阵
  8. [日常工作]非Windows Server 系统远程经常断以及提高性能的方法
  9. Centos8 安装 mariadb 最新版 10.5.x
  10. VMware Workstation pro无法在Windows上运行的解决方法
  11. android tv 8 安装国内app,不花钱解决 Android TV 原生系统国内APP不显示图标
  12. 博途下载触摸屏程序时提示缺少面板映像
  13. Java—求最大公约数和最小公倍数
  14. 第二章 SPSS 的数据管理
  15. 医药箱APP静态小项目
  16. 阿里云常见热门问题解答汇总
  17. Linux嵌入式系统的电子相册代码,基于嵌入式Linux和Qt编程实现数码相框的设计
  18. 理解JDBC/JPA/Mybatis/Hibernate
  19. html英文排版怎么对齐方式,HTML排版中文英文标点不对齐
  20. matlab矩阵 代表什么,matlab中矩阵AB是什么意思

热门文章

  1. 免杀技术有一套(免杀方法大集结)(Anti-AntiVirus)
  2. 【域控管理】域控的必要性
  3. 面向对象设计的23种设计模式
  4. 网页导航栏设计方法和技巧
  5. 定时器计数器实验C语言程序,实验二 单片机定时器和计数器编程
  6. Tableau-盒须图
  7. Java Email
  8. 进入Google十大排名的10个步骤
  9. Python+Django+Mysql个性化图书推荐系统 图书在线推荐系统 基于用户、项目、内容的协同过滤推荐算法(带设计报告)
  10. datasets: mnist