Codeforces 313C Ilya and Matrix【思维】
来源:互联网 发布:ins相机软件下载 编辑:程序博客网 时间:2024/06/05 03:39
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve.
He's got a square 2n × 2n-sized matrix and4n integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that thebeauty of the resulting matrix with numbers is maximum.
The beauty of a 2n × 2n-sized matrix is an integer, obtained by the following algorithm:
- Find the maximum element in the matrix. Let's denote it as m.
- If n = 0, then the beauty of the matrix equalsm. Otherwise, a matrix can be split into 4 non-intersecting2n - 1 × 2n - 1-sized submatrices, then the beauty of the matrix equals the sum of numberm and other four beauties of the described submatrices.
As you can see, the algorithm is recursive.
Help Ilya, solve the problem and print the resulting maximum beauty of the matrix.
The first line contains integer 4n(1 ≤ 4n ≤ 2·106). The next line contains4n integersai(1 ≤ ai ≤ 109) — the numbers you need to arrange in the2n × 2n-sized matrix.
On a single line print the maximum value of the beauty of the described matrix.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin, cout streams or the%I64d specifier.
113
13
41 2 3 4
14
Consider the second sample. You need to arrange the numbers in the matrix as follows:
1 23 4
Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
题目大意:
一个4^n一个矩形,让你将这些数都放在每一个格子中。
定义一个记矩阵的值是这个矩阵中最大价值+一分为四的四个小矩阵的价值。
很显然这是一个递归的过程。
问最大价值。
思路:
我们只要规划出每个数字将会作为矩阵中最大值的次数即可。
很显然,最大值会出现n+1次,然后接下来次大值会出现n次,次次大值也会出现n次,次次次大值也会出现n次,次次次次大值会出现n-1次..........................
依次类推我们可以得到一个规律,统计和即可。
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;int a[3000600];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); sort(a+1,a+n+1); reverse(a+1,a+1+n); __int64 output=0; int now=0; while(1) { int tmp=pow(4,now); now++; if(tmp>n)break; else { for(int i=1;i<=tmp;i++) { output+=a[i]; } } } printf("%I64d\n",output); }}
- Codeforces 313C Ilya and Matrix【思维】
- codeforces 313C. Ilya and Matrix
- CodeForces 313 C.Ilya and Matrix(构造)
- C. Ilya and Matrix
- Codeforce 313C Ilya and Matrix
- Codeforces 313D Ilya and Roads【Dp+思维】
- codeforces 525C Ilya and Sticks
- CodeForces-525C Ilya and Sticks
- Codeforces 525C:Ilya and Sticks
- CodeForces 525C Ilya and Sticks
- Codeforces 525C. Ilya and Sticks
- Codeforces 525C Ilya and Sticks 【数学】
- Codeforces 525C Ilya and Sticks
- CodeForces 525C . Ilya and Sticks
- Codeforces 525C Ilya and Sticks【贪心】
- Codeforces 842C Ilya And The Tree
- Codeforces 842 C Ilya And The Tree
- Codeforces 669C Little Artem and Matrix【思维+暴力】
- p48_合并两个已排序数组
- unity4可以生成apk而unity 5生成不了apk问题
- POJ 2325 Persistent Numbers 笔记
- JVM常见分析工具-1
- windows 系统默认安装路径的修改
- Codeforces 313C Ilya and Matrix【思维】
- 从写方案到见投资人,一步步教你如何拿投资
- 日常英语口语
- ZYNQ系统中实现FAT32文件系统的SD卡读写 之一 硬件介绍
- Hadoop 2.7.2 CentOS x64 环境搭建
- 001. Spring MVC HelloWorld
- AC自动机模板
- poj2794dp
- 自定义View刷新头部,已适配AbsListView、RecyclerView