zoukankan      html  css  js  c++  java
  • CodeForces 313C Ilya and Matrix

    Ilya and Matrix
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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 %I64dspecifier.

    Sample Input

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

    Hint

    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.

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int a[2000005];
     6 
     7 bool cmp(int x,int y)
     8 {
     9     return x>y;
    10 }
    11 int main()
    12 {
    13     int n;
    14     int i,j;
    15     while(scanf("%d",&n)!=EOF)
    16     {
    17         for(i=1;i<=n;i++)
    18         {
    19             scanf("%d",&a[i]);
    20         }
    21         sort(a+1,a+n+1,cmp);
    22         long long s=0;
    23         for(i=0;pow(4,i)<=n+1;i++)
    24         {
    25             for(j=1;j<=pow(4,i);j++)
    26                 s=s+a[j];
    27         }
    28         printf("%I64d
    ",s);
    29     }
    30     return 0;
    31 }
    View Code
  • 相关阅读:
    内置的包装类
    子类继承父类的哪些成员
    JAVA笔记140-使用this语句解决构造器重载相互调用问题
    Java学习
    AngularJs2
    Angular
    检测是否所有的栏位都已经填充了内容了。(可以用来判断动态放置的东西和外加的框是否一致)
    上下各有一个框,框里有元素(点击下面的元素,显示到上面的框里面去,按一定顺序排放)
    Nashorn 在JDK 8中融合Java与JavaScript之力
    2014年Facebook的开源成就
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771527.html
Copyright © 2011-2022 走看看