如何优化
查看原帖
如何优化
1011137
封禁用户楼主2023/10/14 19:58
#include <bits/stdc++.h>
using namespace std;

int t;
int n;
long long ans;
int r[200001], c[200001], b[200001], a[200001];
bool b1[200001];

inline void dfs(int i) {
	if (i == n + 1) {
		long long cnt = 0;
		for (int j = 1; j <= n; j++)
			if (a[j] > b[j])
				cnt += c[j];
			else
				if (a[j] < b[j])
					cnt -= c[j];
		ans = max(ans, cnt);
		return;
	}
	for (int j = 1; j <= n; j++) {
		if (!b1[j]) {
			b1[j] = true;
			b[i] = j;
			dfs(i + 1);
			b[i] = 0;
			b1[j] = false;
		}
	}
}

int main() {
	scanf("%d", &t);
	for (int i = 1; i <= t; i++) {
		ans = 0;
		scanf("%d", &n);
		for (int j = 1; j <= n; j++)
			scanf("%d", &r[j]), a[j] = j;
		for (int j = 1; j <= n; j++)
			scanf("%d", &c[j]);
		dfs(1);
		printf("%lld\n", ans);
	}
}

/*
	Good Luck & Have Fun >_<
       ┏┓   ┏┓
     ┏┛┻━━━┛┻┓
     ┃       ┃
     ┃   ━   ┃
     ┃ ┳┛ ┗┳ ┃
     ┃       ┃
     ┃   ┻   ┃
     ┃       ┃
     ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting
         ┃   ┃    神兽保佑, 代码无bug
         ┃   ┃
         ┃   ┗━━━┓
         ┃     	 ┣┓
         ┃    		 ┏┛
         ┗┓┓┏━┳┓┏┛
           ┃┫┫ ┃┫┫
           ┗┻┛ ┗┻┛     o-| ̄|_
*/

12pts 全超时

2023/10/14 19:58
加载中...