【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name re
ects your task; just add a set of numbers. But you may feel yourselves
condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply
question your erudition. So, lets add some
avor of ingenuity to it.
Addition operation requires cost now, and the cost is the summation of those two to be added. So,
to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways
1 + 2 = 3, cost = 3 1 + 3 = 4, cost = 4 2 + 3 = 5, cost = 5
3 + 3 = 6, cost = 6 2 + 4 = 6, cost = 6 1 + 5 = 6, cost = 6
Total = 9 Total = 10 Total = 11
I hope you have understood already your mission, to add a set of integers so that the cost is minimal.
Input
Each test case will start with a positive number, N (2 N 5000) followed by N positive integers
(all are less than 100000). Input is terminated by a case where the value of N is zero. This case should
not be processed.
Output
For each case print the minimum total cost of addition in a single line.
#include<cstdio>
#include<algorithm>
using namespace std;
int q1[],q2[],h1,h2,t,n;
int get()
{
if (h1>n)
return q2[h2++];
if (h2>t)
return q1[h1++];
if (q1[h1]<=q2[h2])
return q1[h1++];
return q2[h2++];
}
int main()
{
int i,j,k,m,p,q,x,y,z,ans;
while (scanf("%d",&n)==&&n)
{
for (i=;i<=n;i++)
scanf("%d",&q1[i]);
sort(q1+,q1+n+);
ans=;
h1=h2=;
t=;
for (i=;i<n;i++)
{
x=get();
y=get();
ans+=(x+y);
q2[++t]=x+y;
}
printf("%d\n",ans);
}
}
和某年NOIP合并果子一模一样。
思路是贪心,每次取两个最小值相加。
具体实现方法有两种,一种是优先队列,O(nlogn)。
另一种即是本代码。
由于后得到的元素一定比先得到(是得到的,不算原来的)的元素大,所以可以开两个队列。
一个队列放排好序的没动过的元素,另一个放新得到的元素。由于两个队列都是单调递增的,所以取最小元素只要看两个队列头即可。
正当我以为得出了O(n)算法时,突然发现:排序要O(nlogn)。
于是复杂度还是O(nlogn)。
【NOIP合并果子】uva 10954 add all【贪心】——yhx的更多相关文章
- UVa 10954 Add All 贪心
贪心 每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> # ...
- UVA 10954 Add All 哈夫曼编码
题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...
- UVA 10954 Add All 全部相加 (Huffman编码)
题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...
- UVa 10954 Add All(优先队列)
题意 求把全部数加起来的最小代价 a+b的代价为(a+b) 越先运算的数 要被加的次数越多 所以每次相加的两个数都应该是剩下序列中最小的数 然后结果要放到序列中 也就是优先队列了 #inc ...
- UVA 10954 Add All
题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...
- UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)
题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按 ...
- NOIP提高组2004 合并果子题解
NOIP提高组2004 合并果子题解 描述:在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消 ...
- [Swust OJ 352]--合并果子(贪心+队列模拟)
题目链接:http://acm.swust.edu.cn/problem/352/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- Luogu 1090 合并果子(贪心,优先队列,STL运用)
Luogu 1090 合并果子(贪心,优先队列,STL运用) Description 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每 ...
随机推荐
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- Mysql denied for user 'odbc@localhost' || denied for user 'root@localhost'
1. Question Description: 1.1 mysql version: mysql-5.7.11-win64.zip 1.2 if you connect to the mys ...
- hibernate4整合spring3事务问题
本文是作者在对hibernate4+spring3+struts2整合中遇到的一个问题.对s2sh进行了基本的整合搭建以后,就是对事务的控制管理,将hibernate的事务交由spring管理.根据网 ...
- html格式化
解决方法是: 在myeclipse中是这样解决的: 点击 myeclipse菜单栏的 window选项卡,找到下拉 perferences 选项 , 在里面快捷 "搜索" 框里面输 ...
- 优雅输出Javascript从Chrome浏览器
一般前端Web开发好的App中js脚本文件,为了节约流量,都会做最小化,甚至合并压缩处理.但在对于需要Debug已处理过Javascript文件就不太方便了,可读性太差了. 使用Goo ...
- 关于setInterval和setTImeout中的this指向问题
前些天在练习写一个小例子的时候用到了定时器,发现在setInterval和setTimeout中传入函数时,函数中的this会指向window对象,如下例: var num = 0; function ...
- sublimetext Emmet插件(Zen coding)
1.省略div,插件会默认元素为div .container <div class="container"></div> 含糊标签名称,比如不需要指定li, ...
- SAPScript、Smartforms动态打印图像或者背景图片
在利用 SMARTFORMS 进行打印的时候有时候要求输出的图片可能是随着打印内容的不同而不同了,也就是动态输出图片,SMARTFORMS的提供了相关的支持技术,下面是实现截图 1.创建要显示的图片 ...
- ABAP中使用浏览器打开网页
在SAP ABAP中可以在Screen中嵌入Html control打开网页,也可以通过调用本地的IE浏览器打开. 1.在Screen中嵌入Html control的例子,在系统中有,se38:SAP ...
- 高清HDMI编码器|上海视涛科技
HDMI编码器(E300)简介 HDMI编码器(E300)是上海视涛科技出品的高性能HDMI+VGA编码产品.该HDMI+VGA编码器是上海视涛科技完全自主研发,并适用于VGA.DVI.HDMI等信号 ...