Codeforces 313C Ilya and Matrix【思维】

本文介绍了一个关于矩阵美的计算问题,探讨如何通过递归算法排列数值以获得矩阵的最大美观度,并提供了具体的实现思路与代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Ilya and Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 and 4n integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum.

The beauty of a 2n × 2n-sized matrix is an integer, obtained by the following algorithm:

  1. Find the maximum element in the matrix. Let's denote it as m.
  2. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2n - 1 × 2n - 1-sized submatrices, then the beauty of the matrix equals the sum of number m 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.

Input

The first line contains integer 4n (1 ≤ 4n ≤ 2·106). The next line contains 4n integers ai (1 ≤ ai ≤ 109) — the numbers you need to arrange in the 2n × 2n-sized matrix.

Output

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 the cin, cout streams or the %I64d specifier.

Examples
Input
1
13
Output
13
Input
4
1 2 3 4
Output
14
Note

Consider the second sample. You need to arrange the numbers in the matrix as follows:

1 2
3 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);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值