zoukankan      html  css  js  c++  java
  • codeforces C. Ilya and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/313/C

    题目意思:给定 4n 个整数(可以组成 2n × 2n 大小的矩阵),问通过对这些整数进行排列,求出 the resulting maximum beauty of the matrix。这个最大值的定义是这样的:先定义m为所有整数中的最大值。如果n = 0,那么,这个beauty 数就是m;否则把2n × 2n 大小的矩阵划分为  2n - 1 × 2n - 1- 大小的子矩阵,那么 beauty 数是:m + 其余4个子矩阵的beauty数。

          举个例子吧,假设输入的 4n 为16,16个整数为: 1 3 5 9 9 20 3 7 41 30 1 4 5 10 9 7。先排序,然后可以抽象成这样解决:(数字表示第几步要相加的和)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 2*1e6 + 10;
     8 int matrix[maxn];
     9 
    10 int main()
    11 {
    12     int i, j, n;
    13     __int64 ans, sum;
    14     while (scanf("%d", &n) != EOF)
    15     {
    16         for (i = 1; i <= n; i++)
    17             scanf("%d", &matrix[i]);
    18         sort(matrix+1, matrix+1+n);
    19         ans = sum = 0;
    20         for (j=i=1; i <= n; j <<= 2)
    21         {
    22             for ( ; i <= j; i++)  // 就是图中的1,2,3
    23                 sum += matrix[n-i+1];
    24             ans += sum;
    25         }
    26         printf("%I64d
    ", ans);
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    ACM 01背包问题
    HDU 1222(数论,最大公约数)
    HDU 1045(质因数分解)
    HDU 4548(美素数)
    POJ 1458 Common Subsequence
    light oj 1047-neighbor house
    POJ 3903 Stock Exchange
    HDU 1069 monkey an banana DP LIS
    max sum
    ACM比赛
  • 原文地址:https://www.cnblogs.com/windysai/p/3534230.html
Copyright © 2011-2022 走看看