Codeforces #247 (Div. 2) B. Shower Line

来源:互联网 发布:nhj提取软件 编辑:程序博客网 时间:2024/05/16 18:19

暴力题,知道下一个排列:next_permutation()就好做了

b[1]-b[5]为1-5的全排列

则找出b的所有全排列,计算sum = a[b[1]][b[2]] + a[b[2]][b[1]] + a[b[2]][b[3]] + a[b[3]][b[2]] + 2*a[b[3]][b[4]] + 2*a[b[4]][b[3]] + 2*a[b[4]][b[5]]+2*a[b[5]][b[4]];的最大值即可

代码如下:

#include <cstdio>#include <iostream>#include <algorithm>#define MAXN 110#define ll long longusing namespace std;int a[MAXN][MAXN];int b[MAXN];int main(void) {    for(int i=1; i<=5; ++i) {        for(int j=1; j<=5; ++j) {            scanf("%d", &a[i][j]);        }        b[i] = i;    }    ll sum = a[b[1]][b[2]] + a[b[2]][b[1]] + a[b[2]][b[3]] + a[b[3]][b[2]] + 2*a[b[3]][b[4]] + 2*a[b[4]][b[3]] + 2*a[b[4]][b[5]]+2*a[b[5]][b[4]];    ll bestsum = sum;    while(next_permutation(b+1, b+6)) {        sum = a[b[1]][b[2]] + a[b[2]][b[1]] + a[b[2]][b[3]] + a[b[3]][b[2]] + 2*a[b[3]][b[4]] + 2*a[b[4]][b[3]] + 2*a[b[4]][b[5]]+2*a[b[5]][b[4]];      //  cout << "sum = " << sum << endl;        if(sum > bestsum)            bestsum = sum;    }    cout << bestsum << endl;    return 0;}


0 0
原创粉丝点击