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的更多相关文章

  1. UVa 10954 Add All 贪心

    贪心   每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> # ...

  2. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  3. UVA 10954 Add All 全部相加 (Huffman编码)

    题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...

  4. UVa 10954 Add All(优先队列)

    题意  求把全部数加起来的最小代价  a+b的代价为(a+b) 越先运算的数  要被加的次数越多  所以每次相加的两个数都应该是剩下序列中最小的数  然后结果要放到序列中  也就是优先队列了 #inc ...

  5. UVA 10954 Add All

    题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...

  6. UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)

    题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按 ...

  7. NOIP提高组2004 合并果子题解

    NOIP提高组2004 合并果子题解 描述:在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消 ...

  8. [Swust OJ 352]--合并果子(贪心+队列模拟)

    题目链接:http://acm.swust.edu.cn/problem/352/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  9. Luogu 1090 合并果子(贪心,优先队列,STL运用)

    Luogu 1090 合并果子(贪心,优先队列,STL运用) Description 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每 ...

随机推荐

  1. 不经意间网易开源镜像去掉了FreeBSD的镜像

    http://mirrors.163.com/ FreeBSD已经到了这么不招人待见的地步了么? 网易曾经可是FreeBSD的大户啊.

  2. ahjesus 获取当前方法被调用执行的具体位置,包括命名空间和方法

    MethodBase method = ).GetMethod(); string ahjesus = method.ReflectedType.FullName + "." + ...

  3. ANSI X9.19 MAC算法

    /// <summary> /// 获取MAC校验字节数据 /// </summary> /// <param name="bankData"> ...

  4. Follow me to learn how to use mvc template

    Introduction After having gone through many project: Project A Project B Project C I start to write ...

  5. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  6. uml入门之14图与图之间的关系

    1.先奉上整理的14图. 2.其次奉上整理的图之间的6种关系

  7. javascript --- 只继承于原型

    正如上次所述,,出于效率考虑,我们应该尽可能的将一些可重用的属性和方法添加到原型中去. 如果养成了这个好习惯,我们仅仅依靠原型就能顺利的完成继承关系的构建了. 毕竟采用new her()方法将her的 ...

  8. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  9. 异步get请求之Block方法

    #import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...

  10. IOS MenuController的部分操作

    这里我们要实现的将是选择按钮的自定义 综合上一节的随笔,这里给出效果图. ViewController.m // // ViewController.m // CX-MenuController // ...