UVa10264 - The Most Potent Corner

Problem

Every corner of the N-dimensional (1< N<15) unit cube has weight (some positive integer less than 256). We will call two corners neighbouring, if they have common edge. Potency of the corner is the sum of weights of all neighbouring corners. Weights of all the corners are given. You are to determine two neighbouring corners that have the maximum sum of potencies and to output this sum.

Input

The input will consist of several input blocks.Each input block begins withthe integer N, the dimension of thecube. Then there are weights of the corners, one per line in the naturalorder:the first line contains the weight of the corner (0,...0,0,0),the second one - the weight of (0,...,0,0,1),then there is the weight of (0,...,0,1,0),then (0,...,0,1,1), then (0,...,1,0,0),the penultimate line contains the weight of the corner (1,...,1,1,0),the last one - (1,...,1,1,1).

The input is terminated by <EOF>.

Output

For each input block the output line should contain one number,the maximum potencies sum.

Sample Input

3
82
73
8
49
120
44
242
58
2
1
1
1
1

Sample Output

619
4
题意:给出n维立方体单元,求相邻(有公共边就认为是相邻)的两个corner的最大和

思路:先将corner的potency求出来(potency是相邻的corner的权值的和),然后求出相邻corner的potency的最大和。在求potency时,如何判断是否相邻就很关键。对于n,就有pow(2, n)个corner,根据i ^ (1 << j)来求出第i个corner的相邻的corner的序号,其中序号为[0,pow(2, n) - 1]

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct Corner
{
    int sum, weight;
    Corner():sum(0), weight(0){}
};

int n;
vector<Corner> corners;

bool input()
{
    if (scanf("%d", &n) != 1) return false;
    
    int total = 1 << n;
    corners.assign(total, Corner());
    
    for (int i = 0; i < total; i++) {
        scanf("%d", &corners[i].weight);
    }
    
    return true;
}

void solve()
{
    size_t size = corners.size();
    for (size_t i = 0; i < size; i++) {
        for (int j = 0; j < n; j++) {
            corners[i].sum += corners[i ^ (1 << j)].weight;
        }
    }
    
    int result = 0;
    for (size_t i = 0; i < size; i++) {
        for (int j = 0; j < n; j++) {
            result = max(result, corners[i].sum + corners[i ^ (1 << j)].sum);
        }
    }
    
    printf("%d\n", result);
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
    
    while (input()) {
        solve();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值