本文主要是介绍Codeforces #247 (Div. 2) B. Shower Line,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
暴力题,知道下一个排列: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 long
using 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;
}
这篇关于Codeforces #247 (Div. 2) B. Shower Line的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!