数学家伯利亚在《怎样解题》里说过的解题步骤第二步就是迅速想到与该题有关的原型题。(积累的重要性!)

对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的)。当然最后只是输出cost,就没必要建树什么的了。只要理解了huffman算法构造最优二叉树的思路,就按那么想就知道每个a[i]要加多少次了。

当然这道题没想到这些也可以找出规律的,就是一种贪心思想。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
const int maxn=;
int n;
LL ans=,a[maxn];
int main()
{
//freopen("in6.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
sort(a+,a+n+);
for(int i=;i<=n-;i++) ans+=(i+)*a[i];
ans+=n*a[n];
cout<<ans<<endl;
}

Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)的更多相关文章

  1. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  2. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  3. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

  4. Codeforces Round #547 (Div. 3) F 贪心 + 离散化

    https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...

  5. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

  6. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  7. Codeforces Round #303 (Div. 2) D 贪心

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  8. Codeforces Round #263 (Div. 2) A B C

    题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...

  9. Codeforces Round #545 (Div. 2) D 贪心 + kmp

    https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...

随机推荐

  1. 剑指offer 面试49题

    面试49题: 题:丑数 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N ...

  2. 在GCE上安装Apache、tomcat等

    1.安装Apache2.2.3 (虚机的操作系统是CentOS7) sudo yum install wget -y cd /opt sudo wget http://archive.apache.o ...

  3. 使用idea2016导出web项目war包

    第一步配置Web Application:Exploded(已经配置的可以跳到第二步): 打开project structure(默认的快捷键是Ctrl+Alt+Shift+S),依次选择Artifa ...

  4. Windows彻底卸载VMWare虚拟机

    彻底卸载VMWare虚拟机 1.停止VMware相关服务 在服务中将VMware开头的所有服务停止 2.打开VMware安装向导 进入卸载页面 在卸载页面中选中VMware右键点击,进入更改后,页面为 ...

  5. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...

  6. pache—DBUtils框架简介、DbUtils类、QueryRunner类 、ResultSetHandler接口

    Apache—DBUtils框架简介.DbUtils类.QueryRunner类 .ResultSetHandler接口 commons-dbutils 是 Apache 组织提供的一个开源 JDBC ...

  7. Centos6.5安装apache

    1:下载 官网: http://www.apache.org/ 官网下载很慢,从别的地方下载 2:安装 tar -zxvf /usr/local/httpd-2.2.9.tar.gz cd /usr/ ...

  8. linux基础(9)-获取时间

    获取今天日期 date +%Y-%m-%d date +%y-%m-%d date  +%F   获取昨天日期 date -d yesterday +%F date -d -1day +%F     ...

  9. MySQL操作的相关命令

    拷贝表,并且复制两条数据到新表中 create table t_comments_sample2 like t_comments_sample; #拷贝表结构 ,;#复制两条数据 MySQL Work ...

  10. MapReduce-从HBase读取数据处理后再写入HBase

    MapReduce-从HBase读取处理后再写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import o ...