CodeForces 462C Appleman and Toastman 贪心

原题: http://codeforces.com/problemset/problem/462/C

题目:

Appleman and Toastman
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:

Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?

Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.

Output
Print a single integer — the largest possible score.

Sample test(s)
input
3
3 1 5
output
26
input
1
10
output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.

思路:

一堆数,可以无限分,每次分成两部分,分到某部分只有1个的时候,就踢出去,把过程中所有分出来的部分的和加起来,求最大。

比如:3 1 5
第一次是1+3+5这一堆。sum=9。
第二次可以分成1,3+5这两堆,sum=9+1+8=18。
因为1那一堆只剩一个元素了,所以踢掉,我们再分另一堆。
分成3,5两堆,sum=18+3+5=26。

这里我们第二次是还有两种分法的,如果我们先分出3来,sum=9+3+6=18,再把1和5分开,sum=18+1+5=24,。
而先分出5来,sum=9+5+4=18,再把1和3分开,sum=18+1+3=22。

从上面我们可以看出,每次把最小的单独分出来,会得到最大值。
而每次分的时候让一堆只有一个,另一堆尽量最多,可以分更多次,得到的结果也会更大,不信大家和每次平均分比较一下。

既然已经证明了我们每次只把最小的分出来的解法是最优的,那么我们如何计算呢?暴力?10^5个数,再计算每堆的和,时间复杂度是O(n^2),肯定会超时,所以我们可以找规律。

比如1 3 5,显然26=1*2+3*3+5*3,这个不是很好看出规律。
我们再来一组数据,1,2,3,4。
那么我们得到的结果是10 +1+9 +2+7 +3 +4。细心我们又可以发现9=2+3+4,7=3+4,所以这36是等于1*2+2*3+3*4+4*4。
总结规律可得,对于排序后的数组:
sum=a1*2 +a2*3 +a3*4 +……+ a(n-1)*n+an*n’;
所以我们只要按照这样的循环就可以了。

注意:
因为an的值可以取到10^6,n的值可以取到10^5,所以得到的乘积会溢出int,应使用长整型。

代码:

#include <iostream>
#include"stdlib.h"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
typedef long long int lint;
const lint N =1000005;

lint a[N];
int main()
{
   memset(a,0,sizeof(a));
   lint n;
   scanf("%I64d",&n);
   lint ans=0;
   for(lint i=0;i<n;i++)
   {
       scanf("%I64d",&a[i]);
   }
   sort(a,a+n);
   for(lint i=0;i<n;i++)
   {
       ans=ans+a[i]*(i+2);
   }
   ans=ans-a[n-1];
   printf("%I64d\n",ans);
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值